如何在右侧显示android复选框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3156781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:57:38  来源:igfitidea点击:

How to show android checkbox at right side?

androidlayoutcheckbox

提问by UMAR

By default android checkbox shows text at right side and checkbox at left
I want to show checkbox at right side with text at left

默认情况下,android复选框在右侧显示文本,在左侧
显示复选框我想在右侧显示复选框,左侧显示文本

how do I achieve this?

我如何实现这一目标?

采纳答案by xil3

I can't think of a way with the styling, but you could just set the text of the checkbox to nothing, and put a TextView to the left of the checkbox with your desired text.

我想不出样式的方法,但是您可以将复选框的文本设置为空,然后在复选框的左侧放置一个带有所需文本的 TextView。

回答by Hazhir

I think it's too late to answer this question, but actually there is a way to achieve your goal. You just need to add the following line to your checkbox:

我认为现在回答这个问题为时已晚,但实际上有一种方法可以实现您的目标。您只需将以下行添加到您的复选框中:

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

You can use your customized drawable for checkbox as well.

您也可以将自定义的 drawable 用于复选框。

And for a radioButton:

对于单选按钮:

android:button="@null"
android:drawableRight="@android:drawable/btn_radio"

And if you want to do it programmatically:

如果您想以编程方式执行此操作:

Define a layout and name it RightCheckBox and copy the following lines :

定义布局并将其命名为 RightCheckBox 并复制以下行:

<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
    android:text="hello"
    android:layout_width="match_parent" 
    android:layout_height="match_parent"
    android:button="@null"
    android:drawableRight="?android:attr/listChoiceIndicatorMultiple"/>

and when you need to add it programmatically you just need to inflate it to a CheckBox and add it to the root view.

当您需要以编程方式添加它时,您只需要将它膨胀到一个 CheckBox 并将其添加到根视图中。

CheckBox cb = (CheckBox)((LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(R.layout.check_right_checkbox,null);
rootView.addView(cb);

回答by Mahesh Lad

You can do

你可以做

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right|center"//or "center_vertical" for center text
android:layoutDirection="rtl"
android:text="hello" />

Following line is enough

以下行就足够了

android:layoutDirection="rtl"

回答by The Berga

You can add android:layoutDirection="rtl"but it's only available with API 17.

您可以添加,android:layoutDirection="rtl"但它仅适用于 API 17。

回答by Ivo Stoyanov

Just copy this:

只需复制这个:

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Your text:"/>
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            />
    </LinearLayout>

Happy codding! :)

快乐编码!:)

回答by cfh008

Checkbox text may be not aligning to left with

复选框文本可能未与左对齐

android:button="@null"
android:drawableRight="@android:drawable/btn_radio"

in some device. Can use CheckedTextViewas a replacement to avoid the problem,

在某些设备中。可以使用CheckedTextView作为替代来避免这个问题,

<CheckedTextView
    ...
    android:checkMark="@android:drawable/btn_radio" />

and this link will be helpful: Align text left, checkbox right

此链接将有所帮助:将文本左对齐,复选框右对齐

回答by Kerim FIRAT

    <android.support.v7.widget.AppCompatCheckBox
  android:id="@+id/checkBox"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="10dp"
  android:layoutDirection="rtl"
  android:text="text" />`

回答by Sri Kanth

As suggested by @The Berga You can add android:layoutDirection="rtl"but it's only available with API 17.
for dynamic implementation, here it goes

正如@The Berga 所建议的,您可以添加,android:layoutDirection="rtl"但它仅适用于 API 17。
对于动态实现,这里是

chkBox.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);

回答by zeusboy

furthermore from Hazhir imput, for this issue is necessary inject that property in the checkbox xml configuration android:paddingLeft="0dp", this is for avoid the empty space at the checkbox left side.

此外,来自 Hazhir 输入,对于这个问题,有必要在复选框 xml 配置 android:paddingLeft="0dp" 中注入该属性,这是为了避免复选框左侧的空白区域。