我想我在其中一个布局中没有发现错误。我想显示一个复选框及其周围的边框。
我使用 Android Holo Colors Generator 生成图像
我尝试向复选框添加一个形状,这在 Nexus 4 上运行良好,但在任何其他设备上根本没有显示该按钮,因此我添加了一个虚拟布局:
布局:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5sp"
android:layout_marginTop="3sp"
android:background="@drawable/shape_button" >
<CheckBox
android:id="@+id/check"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:button="@drawable/btn_radio_holo_dark_hm" />
</LinearLayout>
形状:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@android:color/transparent"/>
<stroke
android:width="1px"
android:color="@color/gray" />
<corners
android:bottomLeftRadius="5sp"
android:bottomRightRadius="5sp"
android:topLeftRadius="5sp"
android:topRightRadius="5sp" />
</shape>
现在我明白了:
我尝试根据 mdpi/hdpi/xhdpi 设置 minWidth,但按钮从未在每个设备上看起来居中。 fill_parent/wrap_content 不会导致任何更改,还会在布局内移动按钮。
对这里出了什么问题有什么建议吗?
最佳答案
将外部布局更改为 RelativeLayout 并为复选框设置 centerInParent=true
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5sp"
android:layout_marginTop="3sp"
android:background="@drawable/shape_button"
android:padding="2sp">
<CheckBox
android:id="@+id/check"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:button="@drawable/btn_radio_holo_dark_hm"
android:layout_centerInParent="true/>
</RelativeLayout>
关于Android 复选框 - 背景 + 按钮布局 - 尺寸错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14477472/