2016-09-22 70 views
-1

我无法真正找到这个问题的答案,所有我找到的是本地化的问题。比方说,我有这样的:android:如何使视图填充可用空间

image

我可以成功地做到这一点。但是,我想如果“BUTTON2”的可见性设置为GONE,请尽量使BUTTON1的宽度尽可能大。因此,它看起来就像这样:

image

目前这里的非工作代码:

<RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="10dp" 
      android:layout_gravity="center"> 

      <com.rey.material.widget.Button 
       android:id="@+id/button2" 
       style="@style/Material.Drawable.Ripple.Wave.Light" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:onClick="goToOccasion" 
       android:padding="20dp" 
       android:text="@string/join" 
       app:rd_enable="true" 
       app:rd_rippleColor="@android:color/darker_gray" 
       app:rd_rippleType="wave" 
       android:gravity="center" 
       android:layout_alignParentTop="true" 
       android:layout_toEndOf="@+id/joinOccasionBTN" 
       android:layout_marginStart="87dp" 
       android:layout_alignBottom="@+id/joinOccasionBTN" /> 

      <com.rey.material.widget.Button 
       android:id="@+id/joinOccasionBTN" 
       style="@style/Material.Drawable.Ripple.Wave.Light" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:onClick="goToOccasion" 
       android:padding="20dp" 
       android:text="@string/join" 
       app:rd_enable="true" 
       app:rd_rippleColor="@android:color/darker_gray" 
       app:rd_rippleType="wave" 
       android:gravity="center" 
       android:layout_alignParentTop="true" 
       android:layout_alignParentStart="true" /> 
     </RelativeLayout> 

导致此:

image

当BUTTON2是visible,和它的结果与上面的图片相同,但没有button2(“JOIN”)。

+0

我可以问为什么我会得到downvotes?这是一个非常有用的非本地化问题,适用于大量用户 –

回答

2

一个简单的方法是使用LinearLayout中的android:layout_weight属性。

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:layout_margin="10dp" 
     android:layout_gravity="center"> 

    <com.rey.material.widget.Button 
     android:layout_width=0dp 
     android:layout_height="wrap_content" 
     android:layout_weight=1 
     ... 
     /> 

    <com.rey.material.widget.Button 
     android:layout_width=0dp 
     android:layout_height="wrap_content" 
     android:layout_weight=1 
     ... 
     /> 

</LinearLayout> 

这样,如果你的按钮中的一个改变,从visiblegone知名度那么其他按钮将填补剩余的空间。

+0

如果您发现我的问题有用,请将其提升为支持 –

+0

已经做到了。 –

1

而不是relativeLayout,使用gridView并添加它的按钮。 如果你在列上设置你的按钮将采取所有的地方。 如果设置两列,GridView控件会自动将您视图适应两个完全大小的列...

两列:

<GridView 
    android:layout_width="match_parent" 
    android:numColumns="2" 
    android:layout_height="match_parent"> 
    <Button 
     android:id="@+id/button1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
    <Button 
     android:id="@+id/button2" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
</GridView> 

一列:

<GridView 
    android:layout_width="match_parent" 
    android:numColumns="1" 
    android:layout_height="match_parent"> 
    <Button 
     android:id="@+id/button1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
    <Button 
     android:id="@+id/button2" 
     android:visibility:"gone" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
</GridView> 

希望这会有所帮助。

+0

通过添加示例来阐述您的答案;不是为了我自己(*我理解它*),而是为了未来的读者。然后我将删除downvote –

+0

它不起作用。所以我保持-1,对不起。 –

+0

它的作品,只要你等待我们给你完整的蛋糕在你的手中。 – McflyDroid