android - 在 RelativeLayout 内部 Viewgroup 中居中对象

标签 android xml text view centering

我花了几个小时试图修复一个愚蠢的小错误(从我的角度来看)。
它就像我不能在相对布局(在 View 组内)2 Textviews 中居中。
这是一个能够旋转容器的自定义 View 组。
我已经检查了很多其他有类似问题的帖子,但没有任何解决方案有效。

我尝试使用重力、alignText 和我找到的所有组合。
希望有人能看到代码中的错误!

这是我的 XML:

  <com.android.ui.common.RotatableContainer
    android:id="@+id/preview_undo_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="@dimen/module_indicator_height"
    android:background="@android:color/holo_blue_light"
    android:padding="@dimen/content_padding_small"
    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_red_light"
        android:padding="@dimen/content_padding_small"
        android:gravity="center">

        <TextView
            android:id="@+id/undo_info_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="La foto se ha eliminado."
            android:textColor="@android:color/white"
            android:layout_centerInParent="true"

            />

        <TextView
            android:id="@+id/delete_undo_bottom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/content_margin_normal"
            android:layout_toEndOf="@id/undo_info_text"
            android:text="Deshacer"
            android:textAllCaps="true"
            android:textColor="@android:color/white" />

    </RelativeLayout>

</com.android.ui.common.RotatableContainer>

这是我的 CustomContainer 的代码:

 * Rotates first view in this layout by multiple of 90 mAngle.
 * <p/>
 * This layout is supposed to have only one view. Behaviour of the views after the first one
 * is not defined.
 * <p/>
 * Rotate angles can be only multiple of 90.
 * If mAngle is not multiple of 90 it will be reduced to the multiple of 90.
 * For example 89 will be reduced to 0, 91 will be reduced to 90.
 */
public class RotatableContainer extends ViewGroup {

    private int mAngle;

    private final Matrix mRotateMatrix = new Matrix();

    private final Rect mViewRectRotated = new Rect();

    private final RectF mTempRectF1 = new RectF();
    private final RectF mTempRectF2 = new RectF();

    private final float[] mViewTouchPoint = new float[2];
    private final float[] mChildTouchPoint = new float[2];

    private boolean mAngleChanged = true;

    public RotatableContainer(Context context) {
        this(context, null);
    }

    public RotatableContainer(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RotatableContainer(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs);

        final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RotatableContainer);
        final int angleFromAttrs = a.getInt(R.styleable.RotatableContainer_angle, 0);
        mAngle = fixAngle(angleFromAttrs);
        a.recycle();

        setWillNotDraw(false);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        final View view = getView();
        if (view != null) {
            if (Math.abs(mAngle % 180) == 90) {
                //noinspection SuspiciousNameCombination
                measureChild(view, heightMeasureSpec, widthMeasureSpec);
                setMeasuredDimension(
                        resolveSize(view.getMeasuredHeight(), widthMeasureSpec),
                        resolveSize(view.getMeasuredWidth(), heightMeasureSpec));
            } else {
                measureChild(view, widthMeasureSpec, heightMeasureSpec);
                setMeasuredDimension(
                        resolveSize(view.getMeasuredWidth(), widthMeasureSpec),
                        resolveSize(view.getMeasuredHeight(), heightMeasureSpec));
            }
        } else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        if (mAngleChanged || changed) {
            final RectF layoutRect = mTempRectF1;
            final RectF layoutRectRotated = mTempRectF2;
            layoutRect.set(0, 0, r - l, b - t);
            mRotateMatrix.setRotate(mAngle, layoutRect.centerX(), layoutRect.centerY());
            mRotateMatrix.mapRect(layoutRectRotated, layoutRect);
            layoutRectRotated.round(mViewRectRotated);
            mAngleChanged = false;
        }

        final View view = getView();
        if (view != null) {
            view.layout(mViewRectRotated.left, mViewRectRotated.top, mViewRectRotated.right, mViewRectRotated.bottom);
        }
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        canvas.save();
        canvas.rotate(-mAngle, getWidth() / 2f, getHeight() / 2f);
        super.dispatchDraw(canvas);
        canvas.restore();
    }

    @Override
    public ViewParent invalidateChildInParent(int[] location, Rect dirty) {
        invalidate();
        return super.invalidateChildInParent(location, dirty);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        mViewTouchPoint[0] = event.getX();
        mViewTouchPoint[1] = event.getY();

        mRotateMatrix.mapPoints(mChildTouchPoint, mViewTouchPoint);

        event.setLocation(mChildTouchPoint[0], mChildTouchPoint[1]);
        boolean result = super.dispatchTouchEvent(event);
        event.setLocation(mViewTouchPoint[0], mViewTouchPoint[1]);

        return result;
    }

    /**
     * Returns current mAngle of this layout
     */
    public int getAngle() {
        return mAngle;
    }

    /**
     * Sets current mAngle of this layout.
     * If mAngle is not multiple of 90 it will be reduced to the multiple of 90.
     * For example 89 will be reduced to 0, 91 will be reduced to 90.
     */
    public void setAngle(int mAngle) {
        int fixedAngle = fixAngle(mAngle);
        if (this.mAngle != fixedAngle) {
            this.mAngle = fixedAngle;
            mAngleChanged = true;
            requestLayout();
        }
    }

    /**
     * Returns
     */
    public View getView() {
        if (getChildCount() > 0) {
            return getChildAt(0);
        } else {
            return null;
        }
    }

    /**
     * Takes any mAngle, makes it valid one for this view.
     * This means multiple of 90.
     */
    private static int fixAngle(int angle) {
        return (angle / 90) * 90;
    }

}

和我的 View 测量方法:

       public void positionUndoBarContainer(int orientation) {
        Size currentPreviewSize = CameraUtil.getPreviewSizeForAspectRatio(mContext, mAspectRatio);
        if (mUndoContainer.getAngle() != orientation)
            mUndoContainer.setAngle(orientation);

//       
        mUndoContainer.layout(getLeft(), getPreviewTop(), getLeft() + getWidth(), getPreviewTop() + currentPreviewSize.getHeight());
        Log.i(TAG, "measure" + mUndoContainer.getMeasuredHeight() + " normal" + mUndoContainer.getHeight());

        requestLayout();
    }

垂直方向看起来像:enter image description here

在水平方向上是这样的:enter image description here

如您所见,容器已正确旋转并位于中间,但里面的文字不是,我找不到原因>。<

最佳答案

在 RelativeLayout 的中心放置一个 dummy View(一个裸 View,0dp 宽和高)。

然后,对于水平对齐,将一个 TextView 对齐到它的右侧,另一个对齐到它的左侧。
对于垂直 对齐,将一个 TextView 对齐到它的顶部,另一个对齐到它的底部。

关于android - 在 RelativeLayout 内部 Viewgroup 中居中对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36081987/

相关文章:

java - Oncreate 在显示 Activity 之前等待所有方法完成

java - 使用 Retrofit 发送 FormUrlEncoded 时出现问题

java:用于 java 游戏的敌方玩家跟踪 AI

android - 使用Scrolling View做xml布局,如何做出时尚的xml布局设计?

xml - 如何将变量传递到 XML 文件(通过 Terraform)?

Java XML 解析

javascript - 来自外部 .csv 或 .txt 文件的 d3.js 云?

html - 动画发生时元素不移动

Android 通过低功耗蓝牙适配器发送数据

html - 在 HTML Canvas 中添加字母间距