2016-11-23 185 views
0

我想创建一个动态水平布局,其中有3个视图在屏幕上占用相等的空间。当1个视图被隐藏时,剩下的视图应该填满它的空间。Android:创建动态布局

我想用xml来实现这一点,而不是考虑为它编写一些代码。

我能够通过使用LinearLayout和重量来获得3个视图以占用相同的空间以填充屏幕,但无法使其显示,因此如果某些视图被隐藏,视图应填充空间。

我的布局看起来像这样。

<LinearLayout 
    android:layout_width="match_parent" 
    android:weightSum="3" 
    android:layout_height="wrap_content"> 

    <TextView 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:text="This is bob" /> 
    <TextView 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:text="This is bob" /> 
    <TextView 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:text="This is bob" /> 
</LinearLayout> 

我以为它必须已经被某人问过,但我无法找到任何相关的问题。

+0

什么错误????????? – Vijay

+0

使第一个TextView的可视性为View.INVISIBLE – Vijay

+0

@Vijay:当我隐藏1个TextView时。剩余的2个TextViews覆盖了66%和33%的空间。我希望每个textView占用50%的空间,以便100%由剩余的2填充 – SAIR

回答

2

添加

android:orientation="horizontal" 

和主要布局,将工作删除

android:weightSum="3" 

您的布局应该是这样的:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <TextView 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:text="This is bob" /> 
    <TextView 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:text="This is bob" /> 
    <TextView 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:text="This is bob" /> 
</LinearLayout> 
+0

这是在我的脑后,我已经做到了这一点,但在这一刻,但无法记住。那很完美。 – SAIR