我花了好几个小时试图修复一个愚蠢的小错误(从我的角度来看) 它的某些东西就像我不能以相对布局为中心(在Viewgroup内)2 Textviews 它是一个Custom ViewGroup,可以旋转容器 我已经检查了许多其他类似于此问题的帖子,并且没有任何解决方案有效。
我尝试使用gravity,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;
}
}
和我对视图的测量方法:
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();
}
正如您所看到的那样,容器已正确旋转且位于中间,但内部文本却没有,我无法找到原因&gt;。&lt;
答案 0 :(得分:1)
在RelativeLayout的中心放置一个虚拟视图(裸View
,0dp宽和高)。
然后,对于水平对齐方式,将一个TextView对齐到右侧,另一个TextView对齐左侧。 对于垂直对齐方式,将一个TextView对齐到它的顶部,将另一个TextView对齐到它的底部。
答案 1 :(得分:0)
android:gravity="center
中添加RotatableContainer
“