[英]android: How to make view fill available space
我真的找不到這個問題的答案,我發現的只是本地化的問題。 假設我有這個:
我可以成功做到這一點。 但是,我希望如果“ BUTTON2
”的可見性設置為GONE
,則使BUTTON1
的寬度盡可能多。 所以它看起來像這樣:
目前,這是無效代碼:
<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>
結果是:
當button2 visible
,其結果與上圖相同,但沒有button2(“ JOIN”)。
一種簡單的方法是在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>
這樣,如果您的一個按鈕將可見性從visible
更改為gone
則另一個按鈕將填充剩余的空間。
而不是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>
希望這會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.