2016-03-18 25 views
1

我花了幾個小時試圖解決一個愚蠢的小錯誤(從我的角度來看)。
它的東西就像我不能集中在一個相對佈局(在Viewgroup中)2 Textviews。
這是一個可以旋轉容器的自定義ViewGroup。
我已經檢查了很多其他類似於這個問題的帖子,但沒有任何解決方案已經工作。在一個RelativeLayout裏面的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(); 
    } 

在垂直看起來如:enter image description here

而在水平像這樣:enter image description here

正如你所看到的容器正確旋轉和中間但文本里面是不是,我找不到原因>。 <

回答

1

將一個虛設視圖(裸View,0dp寬和高)處的RelativeLayout的中心。

然後,對於橫向對齊,請將一個TextView對齊到它的右側並將另一個TextView對齊到它的左側。
對於垂直對齊方式,請將一個TextView對齊其頂部,將另一個TextView對齊它的底部。

+0

嗨隊友加android:gravity="center「!好主意,但看起來它不工作,問題是不是位置,就像是如果我的相對佈局其無法正常獲取中間。但在其他的方式,全屏幕是藍色的,而相對的(紅色)正確地位於我的可旋轉佈局中間。 但是,當我居中時它總是無序,我不知道爲什麼 –

+1

也刪除'gravity'屬性。正確'對我來說沒有什麼意義,除非你自己沒有繪製自定義的ViewGroup - 在這種情況下會出現一些計算錯誤 –

+0

給我一分鐘,我會上傳一些照片。我自己的觀點。我現在將更新代碼 –

0

RotatableContainer

+0

嘗試了mate,它沒有集中Container的內容 –

相關問題