Android 居中相对视图组内的对象

Android 居中相对视图组内的对象,android,xml,text,view,centering,Android,Xml,Text,View,Centering,我花了几个小时试图修复一个愚蠢的小错误(从我的角度来看)。 这有点像我不能在相对布局中居中(在视图组内)2个文本视图。 这是一个自定义视图组,可以旋转容器。 我已经检查了很多其他类似于这篇文章的问题,但没有任何解决方案有效 我尝试使用重力、对齐文本和我找到的所有组合。 希望有人能看到代码中的错误 以下是我的XML: <com.android.ui.common.RotatableContainer android:id="@+id/preview_undo_container"

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

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

以下是我的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>
在垂直方向上,它看起来像:

在水平方向上,如下所示:

正如您可以看到的容器被正确地旋转和中间,但是文本里面不是,我找不到原因> .p>在相对应的中心放置一个虚拟视图(裸代码>代码> <代码>,0DP宽和高)。p> 然后,对于水平对齐,将一个文本视图与其右侧对齐,另一个文本视图与其左侧对齐。


对于垂直对齐,将一个文本视图与顶部对齐,另一个与底部对齐

可旋转容器中添加
android:gravity=“center

你好!好主意,但看起来好像不行。问题不在于位置,就好像我的相对布局没有正确地找到中间一样。但是在另一方面,全屏是蓝色的,相对的(红色)正好在我可旋转的布局中间。但是当我居中时,它总是杂乱无章,我不知道为什么要删除
gravity
属性<代码>没有正确地取中间位置对我来说没有多大意义。除非你不是自己绘制自定义视图组-在这种情况下会有一些计算错误。给我一分钟,我会上传一些照片。是的,我自己画的。现在我将更新代码,在不分析代码的情况下,我发现TextView的相对位置还有一些问题。1-我会给它们一个固定的宽度,以dp表示,而不是
包装内容
。2-我不会将另一个文本视图设置为
toEndOf
。3-我还将使用
match\u parent
作为RelativeLayout的高度。最后,我使用customview的onlayout()和onmeasure()方法定位文本。你的回答对我帮助很大。太感谢你了!试过了,它没有使容器的内容物居中
 * 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;
    }

}
       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();
    }