2013-10-22 84 views
1

我遇到了修改的android佈局的問題。在我擴展正常的相對佈局後,它將自動調整大小,所有嵌入的其他視圖將消失。在自定義RelativeLayout中丟失視圖

 <namespace.SquareRelativeLayout 
      android:layout_weight="1" 
      android:id="@+id/firstBtn" 
      android:background="#ff0000" 
      android:clickable="true" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 
      <ImageView 
       android:id="@+id/firstBtnImage" 
       android:layout_centerHorizontal="true" 
       android:layout_marginTop="10dp" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/firstBtnDrawable" /> 
      <TextView 
       android:id="@+id/firstBtnLabel" 
       android:layout_alignParentBottom = "true" 
       android:layout_centerHorizontal="true" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="5dp" 
       android:layout_marginBottom="10dp" 
       android:textColor="#ffffff" 
       android:text="@string/firstBtnlabel" 
       android:textStyle="bold" /> 
     </namespace.SquareRelativeLayout> 

但是文本視圖和圖像視圖沒有顯示,即使它們在屏幕上的任何位置。

我感謝每一個後面。

編輯:

namespace namespace 
{ 
    class SquareRelativeLayout : RelativeLayout 
    { 
     public SquareRelativeLayout (Context context, IAttributeSet attrs, int defStyle) : base (context, attrs, defStyle) 
     { 
     } 

     public SquareRelativeLayout (Context context, IAttributeSet attrs) : base (context, attrs) 
     { 
     } 

     public SquareRelativeLayout (Context context) : base (context) 
     { 

     } 

     protected override void OnMeasure (int widthMeasureSpec, int heightMeasureSpec) 
     { 

      //Get canvas width 
      int w = MeasureSpec.GetSize (widthMeasureSpec); 
      base.SetMeasuredDimension (w, w); 
     } 
    } 
} 
+1

請注意,我們應該看看您如何修改實現SquareRelativeLayout的RelativeLayout。 – fasteque

回答

2

我解決了它,讀一百個或更多的網站我有一個更好的瞭解Android的意見和layoutInflater後。

但是這種方案很容易只是更換:

int w = MeasureSpec.GetSize (widthMeasureSpec); 
    base.SetMeasuredDimension (w, w); 

base.OnMeasure (widthMeasureSpec, widthMeasureSpec); 

這使父母的視圖組,並有可能增加孩子的。

+0

偉大的答案我正面臨完全相同的問題:) Thankyou –

相關問題