Align text left, checkbox right

前端 未结 13 2277
孤街浪徒
孤街浪徒 2020-12-29 18:28

I\'m trying to create a layout for a ListView, with a checkbox to the right and some text to the left. The checkbox should be aligned all the way to the right and the TextV

相关标签:
13条回答
  • 2020-12-29 18:58

    Use this as your LIST / RECYCLER ITEM. The below 2 lines are the key.

    android:textDirection="ltr" android:layoutDirection="rtl"

    <CheckBox
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:textDirection="ltr"
                android:layoutDirection="rtl"
                />
    
    0 讨论(0)
  • 2020-12-29 18:59

    for first time you must set button to @null

    android:button="@null"
    

    if you want to onl move android checkbox to right use :

    android:drawableRight="?android:attr/listChoiceIndicatorMultiple"
    

    otherwise if you like to have custom image for checkbox use:

    android:drawableRight="@drawable/selector_check_box"
    

    and for set gravity to right :

    android:gravity="right|center_vertical"
    

    full action for use customize checkbox:

    <?xml version="1.0" encoding="utf-8"?>
    <selector
      xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_window_focused="false" android:state_enabled="true" android:state_checked="true" android:drawable="@drawable/_ics_btn_check_on" />
        <item android:state_window_focused="false" android:state_enabled="true" android:state_checked="false" android:drawable="@drawable/_ics_btn_check_off" />
        <item android:state_enabled="true" android:state_checked="true" android:state_pressed="true" android:drawable="@drawable/_ics_btn_check_on_pressed" />
        <item android:state_enabled="true" android:state_checked="false" android:state_pressed="true" android:drawable="@drawable/_ics_btn_check_off_pressed" />
        <item android:state_enabled="true" android:state_checked="false" android:drawable="@drawable/_ics_btn_check_off" />
        <item android:state_enabled="true" android:state_checked="true" android:drawable="@drawable/_ics_btn_check_on" />
        <item android:state_window_focused="false" android:state_checked="true" android:drawable="@drawable/_ics_btn_check_on_disabled" />
        <item android:state_window_focused="false" android:state_checked="false" android:drawable="@drawable/_ics_btn_check_off_disabled" />
        <item android:state_checked="false" android:drawable="@drawable/_ics_btn_check_off_disabled" />
        <item android:state_checked="true" android:drawable="@drawable/_ics_btn_check_on_disabled" />
    </selector>
    

    images :

    enter image description here enter image description here enter image description here enter image description here enter image description here

    0 讨论(0)
  • 2020-12-29 18:59

    You also remove android:orientation="vertical" since Relative layout does not support this attribute. It is attribute of Linear Layout.

    You can try it

    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:paddingBottom="8dip"
            android:paddingLeft="5dip"
            android:paddingRight="5dip"
            android:paddingTop="8dip" >
    
            <TextView
                android:id="@+id/text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:text="check"
                 />
    
            <CheckBox
                android:id="@+id/chekcbox"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true" />
        </LinearLayout>
    
    0 讨论(0)
  • 2020-12-29 19:01

    After trying to do this myself, I finally did it. You just need to put a TextView and a CheckBox in a horizontal layout, and put the gravity on the layout to the right.

    Here's the code:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:gravity="right"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
            >
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/LabelText"
            />
    <CheckBox
            android:id="@+id/ChecKBox_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    </LinearLayout>
    

    EDIT: I just noticed that you wanted text all the way to the left side of the screen. this will put the check box all the way to the right, and the text right next to it on the left side of the check box.

    Like this:

    ------------------------
               text checkbox
    ------------------------
               text checkbox
    ------------------------
               text checkbox
    ------------------------
    
    0 讨论(0)
  • 2020-12-29 19:03

    Using match_parent for layout_width and android:layoutDirection="rtl" are enough:

    <android.support.v7.widget.AppCompatCheckBox
            android:id="@+id/taxDevRadioButton"
            android:layout_width="match_parent"
            android:layoutDirection="rtl"
            android:layout_height="wrap_content"
            android:layout_below="@+id/divider2"
            android:layout_marginTop="10dp"
            android:text="ELIGIBLE FOR TAX" />
    
    0 讨论(0)
  • 2020-12-29 19:05

    Use android:gravity="start

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Has Diploma:"
            android:textSize="15dp"
            android:id="@+id/checkBox_HasDiploma"
            android:gravity="start"/>
    
    0 讨论(0)
提交回复
热议问题