Android 在切换按钮上设置与宽度相同的高度
我有下面的布局,一行有7个切换按钮。独立于分辨率,我始终有7个按钮在完整的水平宽度。现在我想设置与宽度相同的高度,但它不起作用。 我试着跟随,但没有成功(): 第二次尝试没有成功:Android 在切换按钮上设置与宽度相同的高度,android,togglebutton,Android,Togglebutton,我有下面的布局,一行有7个切换按钮。独立于分辨率,我始终有7个按钮在完整的水平宽度。现在我想设置与宽度相同的高度,但它不起作用。 我试着跟随,但没有成功(): 第二次尝试没有成功: int btnSize=tb_Mo.getLayoutParams().width; tb_Mo.setLayoutParams(new LinearLayout.LayoutParams(btnSize, btnSize)); //and so on for the other B
int btnSize=tb_Mo.getLayoutParams().width;
tb_Mo.setLayoutParams(new LinearLayout.LayoutParams(btnSize, btnSize));
//and so on for the other Buttons...
XML:
等等
我能做什么
解决方案:
<ToggleButton
android:id="@+id/tB_Sa"
android:layout_width="0dp"
android:layout_height="50dp"
android:background="@drawable/btntoggle_selector"
android:textColor="@android:color/white"
android:textOff="OFF"
android:textOn="ON"
android:layout_weight="1"/>
您应该使用android:layout\u width=“0dp”来创建您的
s。也许这会奏效。谢谢你的回答,但不幸的是它不起作用。你能提供完整的xml布局吗?如果家长占据了整个屏幕,并且现在只有这些内容,那么您可能会得到这种效果。也可能是您的布局高度
设置为wrap\u content
,这可能会阻止Java函数的重写。尝试将其设置为特定的dp
。是的,它工作了!!!我把它设为50分。谢谢!!!
<ToggleButton
android:id="@+id/tB_Mo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/btntoggle_selector"
android:textColor="@android:color/white"
android:textOff="OFF"
android:textOn="ON"
android:layout_weight="1"/>
<ToggleButton
android:id="@+id/tB_Di"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/btntoggle_selector"
android:textColor="@android:color/white"
android:textOff="OFF"
android:textOn="ON"
android:layout_weight="1"/>
<ToggleButton
android:id="@+id/tB_Mi"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/btntoggle_selector"
android:textColor="@android:color/white"
android:textOff="OFF"
android:textOn="ON"
android:layout_weight="1"/>
and so on ....
<ToggleButton
android:id="@+id/tB_Sa"
android:layout_width="0dp"
android:layout_height="50dp"
android:background="@drawable/btntoggle_selector"
android:textColor="@android:color/white"
android:textOff="OFF"
android:textOn="ON"
android:layout_weight="1"/>
int btnSize=tb_Mo.getWidth();
tb_Mo.setHeight(btnSize);