Android 复选框 - 背景 + 按钮布局 - 尺寸错误

标签 android layout checkbox shapes

我想我在其中一个布局中没有发现错误。我想显示一个复选框及其周围的边框。

我使用 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>

现在我明白了:

Layout on Devices

我尝试根据 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/

相关文章:

android - 使用 Intent 发送的 Action 从 Android 应用程序共享图像它不起作用 :(

android - 在 Delphi 中实现 Java/Android 的回调/监听器

css - 使用 Flex 进行 3 列不同大小的布局

c# - WPF 页面调整大小

html - 我的布局有什么问题?带有 iframe 的固定 header

android - 将 RIGHT 外连接转换为左外连接 SQLite

android - 如何访问内部存储的图像

jquery - 复选框列表,按字母顺序排列在自上而下的列中

php - 在选择类别上使用单选输入而不是复选框 - Wordpress

php - 识别选中的复选框序列然后保存到 mysql 数据库