中心CheckBox视图

Cal*_*laf 5 android android-styles

如果除了(或代替)CheckBoxes之外你对RadioButtons感兴趣,请参阅问题.

尽管存在

<item name="android:layout_gravity">center_horizontal</item>
Run Code Online (Sandbox Code Playgroud)

在样式文件中,两个复选框不居中,但显示为"左对齐".

非中心

RES /布局/ activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/MyLL"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <CheckBox
        android:id="@+id/cb1"
        style="@style/CB_style" />

    <CheckBox
        android:id="@+id/cb2"
        style="@style/CB_style" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

RES /值/ styles.xml

<style name="CB_style" parent="@android:style/TextAppearance.Medium">
    <item name="android:layout_gravity">center_horizontal</item>
    <item name="android:layout_weight">1</item>
    <item name="android:gravity">center</item>
    <item name="android:checked">false</item>
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
</style>
Run Code Online (Sandbox Code Playgroud)

Cap*_*tan 1

你有 android:layout_weight = 1。所以你的复选框填充了屏幕的所有宽度。从样式中删除 android:layout_weight 并在复选框之间添加边距。

复选框中的重力不会影响内部勾选按钮,只会影响文本。

编辑

好的,试试这个:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/MyLL"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="0dp"
        android:layout_height="5dp"
        android:layout_weight="1"/>

    <CheckBox
        android:id="@+id/cb1"
        style="@style/CB_style" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="5dp"
        android:layout_weight="1"/>

    <CheckBox
        android:id="@+id/cb2"
        style="@style/CB_style" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="5dp"
        android:layout_weight="1"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

并从样式中删除layout_weight。

另一种方法是创建自定义复选框。但我认为这个问题太复杂了。