【问题标题】:android how to set checkbox icon on rightside?android如何在右侧设置复选框图标?
【发布时间】:2014-04-14 14:49:02
【问题描述】:

我正在制作一个 api,其中我在我的 xml 文件中使用复选框。问题是我的复选框图标显示在左侧。但我想将我的复选框移动到文本“男性”或“的右侧女性”。像这样..

但目前我越来越喜欢这种观点..

我想要右侧的复选图标选项..,我已经使用了此代码..这是我的 xml..

<RelativeLayout
    android:id="@+id/header_layout"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:background="#43A6E5" >

    <TextView
        android:id="@+id/tv_header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Earn 1 PTS for every Chat you Receive"

        android:textColor="#FFF200"
        android:textSize="18dp" />
</RelativeLayout>

<View
    android:id="@+id/view1"
    android:layout_width="fill_parent"
    android:layout_height="6dp"
    android:layout_below="@+id/header_layout"
    android:layout_centerHorizontal="true"
    android:background="#46B1F6" />

<RelativeLayout
    android:id="@+id/edt_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/view1"
    android:background="#46B1F6" >

    <EditText
        android:id="@+id/et_mail"
        style="@style/EdittextStyle"
        android:layout_alignLeft="@+id/et_name"
        android:layout_alignRight="@+id/et_name"
        android:layout_below="@+id/et_name"
        android:background="@drawable/rounded_edittext"
        android:text="  Your Email" >

        <requestFocus />
    </EditText>

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="80dp"
        android:layout_height="35dp"
        android:layout_alignLeft="@+id/et_mail"
        android:layout_below="@+id/et_mail"
        android:layout_marginTop="33dp"
        android:background="@drawable/rounded_edittext" />

    <EditText
        android:id="@+id/editText1"
        style="@style/EdittextStyle"
        android:layout_alignBottom="@+id/spinner1"
        android:layout_alignRight="@+id/et_mail"
        android:layout_marginLeft="14dp"
        android:layout_toRightOf="@+id/spinner1"
        android:background="@drawable/rounded_edittext"
        android:text="  Your Phone"
        android:textColor="#ffffff" />

    <EditText
        android:id="@+id/et_name"
        style="@style/EdittextStyle"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="29dp"
        android:background="@drawable/rounded_edittext"
        android:text="  Your Name" />

    <!-- android:background="@drawable/rounded_edittext" /> -->

</RelativeLayout>

<RelativeLayout
    android:id="@+id/checkbox_layoyt"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/edt_layout"
    android:layout_marginTop="34dp" >

    <CheckBox
        android:id="@+id/cb_male"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="29dp"
        android:background="@drawable/rounded_edittext"
        android:text=" Male"
        android:textColor="#ffffff"
        android:textSize="18dp" />


    <CheckBox
        android:id="@+id/cb_female"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="18dp"
        android:layout_toRightOf="@+id/cb_male"
        android:background="@drawable/rounded_edittext"
        android:text=" Female"
        android:textColor="#ffffff"
        android:textSize="18dp" />
</RelativeLayout>

<Button
    android:id="@+id/button1"
    android:layout_width="250dp"
    android:layout_height="40dp"
    android:layout_below="@+id/checkbox_layoyt"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="27dp"
    android:background="@drawable/mybutton"
    android:gravity="center"
    android:text="Create Account"
    android:textColor="#ffffff"
    android:textSize="22dp" />

<TextView
    android:id="@+id/textview_agree"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="16dp"
    android:gravity="center"
    android:text="I agree to the terms and conditions"
    android:textColor="#ffffff"
    android:textSize="14dp" />

谢谢你..

【问题讨论】:

  • 有一个没有文本的复选框并将文本视图添加到该复选框的右侧

标签: android xml checkbox


【解决方案1】:

试试这个.. 你可以使用 CheckedTextView 而不是 CheckBox 来满足你的需要.. 而且它几乎具有类似于 CheckBox 的所有属性,所以你可以也可以通过 java 检查/取消选中它,而不使用任何外部可绘制图像并根据不同的条件更改它们的源,这可能会证明代价高昂并且有时会变得一团糟..

<RelativeLayout
        android:id="@+id/checkbox_layoyt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/edt_layout"
        android:layout_marginTop="34dp" >

        <CheckedTextView
            android:id="@+id/cb_male"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="29dp"
            android:checkMark="?android:attr/listChoiceIndicatorMultiple"
            android:checked="false"
            android:gravity="center"
            android:text=" Male"
            android:textColor="#000000"
            android:textSize="18dp" />

        <CheckedTextView
            android:id="@+id/cb_female"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="18dp"
            android:layout_toRightOf="@+id/cb_male"
            android:checkMark="?android:attr/listChoiceIndicatorMultiple"
            android:checked="false"
            android:gravity="center"
            android:text=" Female"
            android:textColor="#000000"
            android:textSize="18dp" />
    </RelativeLayout>

【讨论】:

    【解决方案2】:

    在您的复选框中使用以下属性:

    Set android:button="@null";
    Set android:drawableRight="@drawable/YOUR_CHECKBOX_BUTTON"
    

    希望它会奏效。

    【讨论】:

    • 在 Pre-Lollipop 设备上使用 AppCompat 主题时,这将不允许小部件着色。
    • 使用CheckedTextView 似乎没有AppCompat 主题问题 - 您可以将?android:attr/listChoiceIndicatorMultiple 用于checkMark 属性,它会正确着色。经过大量的反复试验后发现了这一点......
    【解决方案3】:
     <RelativeLayout
                    android:id="@+id/xml_associate_relative_radio_career"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingBottom="@dimen/common_left_right_margin"
                    android:paddingTop="@dimen/common_left_right_margin" >
    
                    <ImageView
                        android:id="@+id/xml_associate_img_career"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:gravity="center_vertical"
                        android:alignparentRight="true"
                        android:src="@drawable/sel_radiobtn_click" />
    
                    <TextView
                        android:id="@+id/xml_associate_txt_career"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:layout_toLeftOf="@+id/xml_associate_img_career"
                        android:gravity="center_vertical"
                        android:paddingLeft="@dimen/common_left_right_margin"
                        android:paddingRight="@dimen/common_padding_left_right"
                        android:text="@string/associate_career"
                        android:textColor="@color/common_color_blackdarkergrey"
                        android:textSize="@dimen/common_font_16sp" />
                </RelativeLayout>
    

    请使用此代码.....imageview 设置复选框图像..并且您使用了相对布局单击事件。

    【讨论】:

      【解决方案4】:

      只需将其添加到您的 Checkbox XML 代码中:

      android:button="@null"
      android:drawableRight="?android:attr/listChoiceIndicatorMultiple"
      

      这对我有用。

      【讨论】:

        【解决方案5】:

        使用这个。

        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
        
        <CheckBox
            android:id="@+id/checkBox1"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="68dp"
            android:paddingRight="100dp" />
        
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="32dp"
            android:text="Male"
            android:textAppearance="?android:attr/textAppearanceSmall" />
        
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/checkBox1"
            android:layout_alignBottom="@+id/checkBox1"
            android:layout_centerHorizontal="true"
            android:text="Female"
            android:textAppearance="?android:attr/textAppearanceSmall" />
        
        <CheckBox
            android:id="@+id/checkBox2"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/textView2" />
        
        </RelativeLayout>
        

        【讨论】:

          【解决方案6】:

          最简单的方法是:

          <RelativeLayout
          android:id="@+id/checkbox_layoyt"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_below="@+id/edt_layout"
          android:layout_marginTop="34dp" >
           <CheckBox
              android:id="@+id/checkBox1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentRight="true"
              android:layout_alignParentTop="true"
              android:layout_marginRight="31dp"
              android:layout_marginTop="43dp"/>
          
          <CheckBox
              android:id="@+id/checkBox2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignBaseline="@+id/checkBox1"
              android:layout_alignBottom="@+id/checkBox1"
              android:layout_centerHorizontal="true" />
          
          <TextView
              android:id="@+id/textView1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignBaseline="@+id/checkBox2"
              android:layout_alignBottom="@+id/checkBox2"
              android:layout_toRightOf="@+id/button1"
              android:text="@string/male" />
          
          <TextView
              android:id="@+id/textView2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignBaseline="@+id/checkBox1"
              android:layout_alignBottom="@+id/checkBox1"
              android:layout_toLeftOf="@+id/checkBox1"
              android:text="@string/female" />
          </RelativeLayout>
          

          Output

          【讨论】:

            【解决方案7】:

            将此设置为您的按钮:

            android:drawableRight="@drawable/YOUR_CHECKBOX_BUTTON"
            

            【讨论】:

              猜你喜欢
              • 2020-05-23
              • 2011-03-10
              • 2011-06-27
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2023-03-04
              • 1970-01-01
              相关资源
              最近更新 更多