2013-10-22 81 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 –

相关问题