[英]How to fit an array of buttons in a linear layout perfectly
I am making an alarm clock app in which users select days in a week in which alarm will go off. 我正在制作一个闹钟应用程序,用户可以在其中选择一周中哪几天响起闹钟。 For that I am using 7 toggle buttons inside a linear layout.
为此,我在线性布局中使用了7个切换按钮。 And What I expect to see is this
我希望看到的是
But I'm only seeing this in tablet layouts. 但是我只能在平板电脑布局中看到这一点。 For many phone layouts I am seeing this
对于许多电话布局,我看到了这一点
That is last button is not visible, buttons are not properly aligned and have newline. 那是最后一个按钮不可见,按钮未正确对齐并带有换行符。 How to fix this?
如何解决这个问题? My xml code [EDITED] :
我的xml代码[编辑] :
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"
android:orientation="horizontal"
android:weightSum="7" >
<ToggleButton
android:id="@+id/toggleButtonmon"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:textOff="Mon"
android:textOn="Mon" />
<ToggleButton
android:id="@+id/toggleButtontue"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:textOff="Tue"
android:textOn="Tue"
android:layout_weight="1"/>
<ToggleButton
android:id="@+id/toggleButtonwed"
android:layout_height="wrap_content"
android:textOff="Wed"
android:textOn="Wed"
android:layout_weight="1"
android:layout_width="0dp"/>
<ToggleButton
android:id="@+id/toggleButtonthu"
android:layout_height="wrap_content"
android:textOff="Thu"
android:textOn="Thu"
android:layout_weight="1"
android:layout_width="0dp" />
<ToggleButton
android:id="@+id/toggleButtonfri"
android:layout_height="wrap_content"
android:textOff="Fri"
android:textOn="Fri"
android:layout_width="0dp"
android:layout_weight="1"
/>
<ToggleButton
android:id="@+id/toggleButtonsat"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textOff="Sat"
android:textOn="Sat"
android:layout_width="0dp"/>
<ToggleButton
android:id="@+id/toggleButtonsun"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textOff="Sun"
android:textOn="Sun"
android:layout_width="0dp" />
</LinearLayout>
Firstly add android:orientation="horizontal"
to your LinearLayout
. 首先,将
android:orientation="horizontal"
到LinearLayout
。
Secondly - set android:layout_width="0dp"
for each ToggleButton
in order for the weight attribute to take effect. 其次-为每个
ToggleButton
设置android:layout_width="0dp"
,以使weight属性生效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.