我在这里遇到了问题。我需要调整线性布局中心的自定义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" />
要使您的复选框居中,您可以尝试将其父级LinearLayout
宽度更改为android:layout_width="wrap_content"
,尤其是当它是ConstraintLayout
的子级时,如果不是,则添加android:layout_gravity="center"
。
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="wrap_content" // instead of match_parent
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_gravity="center" // instead of android:gravity
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="@+id/tv_ticket"
app:layout_constraintStart_toStartOf="@+id/tv_ticket"
app:layout_constraintTop_toBottomOf="@+id/tv_ticket">
为什么在这里使用LinerLayout?您可以删除它并使用constraintlayout链工具来保持平面视图层次结构。检查一下:qazxsw poi