在linearlayout中心对齐自定义复选框按钮

时间:2019-03-13 20:46:16

标签: android android-layout android-custom-view

我在这里遇到问题。我需要调整线性布局中心的自定义4复选框。已经尝试使用重力,layout_weight在xml项中设置重力。但是,按钮始终向左对齐。这是我已实现的视图模式:

<androidx.constraintlayout.widget.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"
tools:context="">

<TextView
    android:id="@+id/tv_amount_tickets"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="32dp"
    android:layout_marginEnd="16dp"
    android:text="@string/select_amount_tickets"
    android:textSize="16sp"
    android:textStyle="bold"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<EditText
    android:id="@+id/til_amount_tickets"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:inputType="number"
    android:imeOptions="actionDone"
    android:maxLines="1"
    android:hint="@string/amount_placeholder"
    app:layout_constraintEnd_toEndOf="@+id/tv_amount_tickets"
    app:layout_constraintStart_toStartOf="@+id/tv_amount_tickets"
    app:layout_constraintTop_toBottomOf="@+id/tv_amount_tickets"
    />

<TextView
    android:id="@+id/tv_ticket"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="32dp"
    android:textSize="16sp"
    android:textStyle="bold"
    android:text="@string/tv_ticket_type"
    app:layout_constraintEnd_toEndOf="@+id/til_amount_tickets"
    app:layout_constraintStart_toStartOf="@+id/til_amount_tickets"
    app:layout_constraintTop_toBottomOf="@+id/til_amount_tickets" />

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="16dp"
    android:gravity="center"
    android:orientation="horizontal"
    app:layout_constraintEnd_toEndOf="@+id/tv_ticket"
    app:layout_constraintStart_toStartOf="@+id/tv_ticket"
    app:layout_constraintTop_toBottomOf="@+id/tv_ticket">

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:foregroundGravity="center"
        android:button="@drawable/checkbox_circle_check_selector" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:foregroundGravity="center"
        android:button="@drawable/checkbox_circle_check_selector" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:foregroundGravity="center"
        android:button="@drawable/checkbox_circle_check_selector" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:foregroundGravity="center"
        android:button="@drawable/checkbox_circle_check_selector" />

</LinearLayout>

selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/checkbox_circle_check"
    android:state_checked="false"/>
<item android:drawable="@drawable/checkbox_circle_check_selected"
    android:state_checked="true"/>
<item android:drawable="@drawable/checkbox_circle_check"/>

正常复选框

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
    <shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
        <corners android:radius="3dp" />
        <solid android:color="#FFFFFFFF" />
        <stroke android:width="0.5dp" android:color="#FF8492A6"/>
    </shape>
</item>
<item android:drawable="@drawable/ic_circle_check" />

选中的复选框

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
    <shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
        <corners android:radius="3dp"/>
        <solid android:color="@color/colorPrimary"/>
        <stroke android:width="0.5dp" android:color="#FF8492A6"/>
    </shape>
</item>
<item android:drawable="@drawable/ic_circle_check_white" />

2 个答案:

答案 0 :(得分:1)

要使您的复选框居中,您可以尝试将其父项<ul> <?php if(have_posts()) : ?> <?php while(have_posts()) : the_post(); ?> <li><?php the_title(); ?></li> <?php endwhile; ?><?php endif; ?> </ul> 的宽度更改为LinearLayout,尤其是当其为android:layout_width="wrap_content"的子项时,如果不是,则添加ConstraintLayout

android:layout_gravity="center"

答案 1 :(得分:1)

为什么在这里使用LinerLayout?您可以删除它,并使用约束布局链工具保持平面视图层次结构。检查以下内容:https://constraintlayout.com/basics/create_chains.html