使所有复选框都在同一垂直列中

时间:2018-06-20 17:11:55

标签: android xml

我正在尝试在android项目的XML文件的同一垂直列中放置所有复选框。 结果和代码已给出;

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center">

    <CheckBox
        android:id="@+id/cannamon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cinnamon"
        android:textSize="18sp" />

    <CheckBox
        android:id="@+id/nutmeg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Nutmeg"
        android:textSize="18sp" />

    <CheckBox
        android:id="@+id/nutella"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Nutella"
        android:textSize="18sp" />

    <CheckBox
        android:id="@+id/whipped_cream"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Whipped Cream"
        android:textSize="18sp" />
</LinearLayout>

enter image description here

我认为线性布局的约束是正确的。 还是应该使用网格布局?

4 个答案:

答案 0 :(得分:0)

很多方法可以做到这一点。这是一个示例:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="left">

        <CheckBox
            android:id="@+id/cannamon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Cinnamon"
            android:textSize="18sp" />

        <CheckBox
            android:id="@+id/nutmeg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Nutmeg"
            android:textSize="18sp" />

        <CheckBox
            android:id="@+id/nutella"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Nutella"
            android:textSize="18sp" />

        <CheckBox
            android:id="@+id/whipped_cream"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Whipped Cream"
            android:textSize="18sp" />
    </LinearLayout>

</RelativeLayout>

答案 1 :(得分:0)

您当前正在做的是将每个CheckBox视图的中心与中心对齐。由于每个CheckBox的宽度都不相同,因此这些框不会对齐。您可以做的是将所有内容向左对齐,然后将LinearLineLayout包裹在另一个居中的位置。这是一个示例:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <CheckBox
            android:id="@+id/cannamon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Cinnamon"
            android:textSize="18sp"/>

        <CheckBox
            android:id="@+id/nutmeg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Nutmeg"
            android:textSize="18sp"/>

        <CheckBox
            android:id="@+id/nutella"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Nutella"
            android:textSize="18sp"/>

        <CheckBox
            android:id="@+id/whipped_cream"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Whipped Cream"
            android:textSize="18sp"/>
    </LinearLayout>

答案 2 :(得分:0)

从xml代码中的linearlayout中删除android:gravity =“ center”

答案 3 :(得分:0)

您应该使用ConstraintLayout。我想您想实现以下目标:

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

    <android.support.constraint.ConstraintLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <CheckBox
            android:id="@+id/checkBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:text="CheckBox"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <CheckBox
            android:id="@+id/checkBox2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:text="CheckBoxTest"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/checkBox" />

    </android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout

>

enter image description here