Align Checkbox to center of the ListView item

落爺英雄遲暮 提交于 2020-06-26 07:34:32

问题


The following layout is used in my app to show cliplists.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/ClipNo"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Clip No"
        android:textSize="20sp"
        android:gravity="center"
        android:layout_weight="1"/>

    <TextView
        android:id="@+id/ClipTime"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Time"
        android:textSize="20sp"
        android:gravity="center"
        android:layout_weight="1"/>

    <TextView
        android:id="@+id/ClipDate"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Date"
        android:textSize="20sp"
        android:gravity="center"
        android:layout_weight="1"/>

    <CheckBox android:id="@+id/ClipMark"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1" />

</LinearLayout>

The below image shows the output of the layout

How can i arrange the 'CheckBox' to center of the 'Mark' Header

Thanks in advance


回答1:


Because you have added android:layout_weight="1"

Change your below code

 <CheckBox 
        android:id="@+id/ClipMark"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1" />

To

<LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_weight="1">

        <CheckBox 
            android:id="@+id/ClipMark"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

</LinearLayout>



回答2:


I placed the CheckBox inside a LinearLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/ClipNo"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Clip No"
        android:textSize="20sp"
        android:gravity="center"
        android:layout_weight="1"/>

    <TextView
        android:id="@+id/ClipTime"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Time"
        android:textSize="20sp"
        android:gravity="center"
        android:layout_weight="1"/>

    <TextView
        android:id="@+id/ClipDate"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Date"
        android:textSize="20sp"
        android:gravity="center"
        android:layout_weight="1"/>
    <LinearLayout
        android:gravity="center"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content">

        <CheckBox android:id="@+id/ClipMark"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    </LinearLayout>
</LinearLayout>



回答3:


Create a LinearLayout as PARENT.

    <LinearLayout
            android:layout_width="0dp"
            android:gravity="center"
            android:layout_weight="1"
            android:layout_height="wrap_content" >

        <CheckBox
            android:id="@+id/ClipMark"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"/>

   </LinearLayout>



回答4:


If you check the device Layout Boundaries, the CheckBox is rightly aligned to center as per your current layout. Add the CheckBox to a layout(LinearLayout example) and implement the layout.
It should work.
TIP: Refer: CheckBox official documentation




回答5:


Try this add your CheckBox inside a RelativeLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/ClipNo"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Clip No"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/ClipTime"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Time"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/ClipDate"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Date"
        android:textSize="20sp" />

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <CheckBox
            android:id="@+id/ClipMark"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    </RelativeLayout>

</LinearLayout>



回答6:


try this one it had worked for me

  <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/interested_item_checkbox"
            android:layout_centerInParent="true"
            style="@style/interested_item_textview"
            />
    </RelativeLayout>



回答7:


Replace checkbox xml code

<RelativeLayout
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content">

<CheckBox
    android:id="@+id/ClipMark"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true" />
</RelativeLayout>



回答8:


Ich just put a negative Margin-Value to my Checkbox

<style name="details_checkbox">
    <item name="android:scaleX">0.80</item>
    <item name="android:scaleY">0.80</item>
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_alignParentEnd">true</item>
    <item name="android:layout_marginTop">-4dp</item>
</style>


来源:https://stackoverflow.com/questions/47386980/align-checkbox-to-center-of-the-listview-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!