如何在右侧显示androidcheckbox?

默认情况下,androidcheckbox在右侧显示文本,在左侧显示checkbox
我想在右侧显示checkbox,并在左侧显示文字

我怎么做到这一点?

我无法想象样式的方式,但是您可以将checkbox的文本设置为空,并将TextView放在checkbox的左侧,并带有所需的文本。

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

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

您也可以使用您的自定义绘制checkbox。

对于radioButton:

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

如果你想以编程方式做到这一点:

定义一个布局并将其命名为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"/> 

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

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

你可以添加android:layoutDirection="rtl"但它只适用于API 17。

只需复制这个:

  <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> 

快乐的编码! 🙂

你可以做

 <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" /> 

以下线就够了

 android:layoutDirection="rtl" 

checkbox文本可能不与左alignment

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

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

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

这个链接会有帮助: 将文本左alignment,checkbox右alignment

此外从Hazhirinput,这个问题是必要的注入该属性在checkboxxmlconfigurationandroid:paddingLeft =“0dp”,这是为了避免checkbox左侧的空白空间。

正如@The Berga所build议的那样,您可以添加android:layoutDirection="rtl"但它只适用于API 17。
对于dynamic实现,在这里

 chkBox.setLayoutDirection(View.LAYOUT_DIRECTION_RTL); 

对使用CheckedTextView的这个问题添加另一个答案如果有人试图以编程方式做。 它也可以select使用自定义图像checkbox。 并且可以在单个视图中完成

 final CheckedTextView checkBox = new CheckedTextView(getApplicationContext()); checkBox.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); checkBox.setId(1); checkBox.setCheckMarkDrawable(android.R.drawable.checkbox_off_background); checkBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (checkBox.isChecked()){ checkBox.setChecked(false); checkBox.setCheckMarkDrawable(android.R.drawable.checkbox_off_background); }else{ checkBox.setChecked(true); checkBox.setCheckMarkDrawable(android.R.drawable.checkbox_on_background); } } }); checkBox.setTextColor(Color.BLACK); checkBox.setGravity(Gravity.LEFT); checkBox.setText("hi"); 

如果你想启动,从XML –

 <CheckedTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:checkMark="@android:drawable/checkbox_off_background" android:checked="false" android:text="Hi from xml"/> 

下面的链接演示了如何通过设置右边的drawable来在右侧渲染一个具有animationcheckbox的seveveral标准Android视图对象。

设置背景以获得连锁效果。

[右侧和左侧链接到示例checkbox的网站。] [1] http://landenlabs.com/android/uicomponents/uicomponents.html#checkbox

  <Button android:id="@+id/p2Button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/transparent_ripple" android:drawableRight="@drawable/checkline" android:gravity="left|center_vertical" android:text="Button" android:textAllCaps="false" android:textColor="@android:color/white" android:textSize="@dimen/buttonTextSize" /> <android.support.v7.widget.AppCompatButton android:id="@+id/p2Button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/transparent_ripple" android:drawableRight="@drawable/checkline" android:gravity="left|center_vertical" android:text="AppCompatButton" android:textAllCaps="false" android:textColor="@android:color/white" android:textSize="@dimen/buttonTextSize" /> <TextView android:id="@+id/p2TextView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/transparent_ripple" android:drawableRight="@drawable/checkline" android:gravity="left|center_vertical" android:hapticFeedbackEnabled="true" android:text="TextView" android:textColor="@android:color/white" android:textSize="@dimen/buttonTextSize" /> <android.support.v7.widget.AppCompatTextView android:id="@+id/p2TextView2" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/transparent_ripple" android:drawableRight="@drawable/checkline" android:gravity="left|center_vertical" android:hapticFeedbackEnabled="true" android:text="AppCompatTextView" android:textColor="@android:color/white" android:textSize="@dimen/buttonTextSize" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@android:color/white" /> <CheckBox android:id="@+id/p2Checkbox1" android:layout_width="match_parent" android:layout_height="@dimen/buttonHeight" android:background="@drawable/transparent_ripple" android:button="@null" android:checked="true" android:drawableRight="@drawable/checkline" android:gravity="left|center_vertical" android:text="CheckBox" android:textColor="@android:color/white" android:textSize="@dimen/buttonTextSize" /> <android.support.v7.widget.AppCompatCheckBox android:id="@+id/p2Checkbox2" android:layout_width="match_parent" android:layout_height="@dimen/buttonHeight" android:background="@drawable/transparent_ripple" android:button="@null" android:checked="true" android:drawableRight="@drawable/checkline" android:gravity="left|center_vertical" android:text="AppCompatCheckBox" android:textColor="@android:color/white" android:textSize="@dimen/buttonTextSize" /> <android.support.v7.widget.AppCompatCheckedTextView android:id="@+id/p2Checkbox3" android:layout_width="match_parent" android:layout_height="@dimen/buttonHeight" android:background="@drawable/transparent_ripple" android:checkMark="@drawable/checkline" android:checked="true" android:gravity="left|center_vertical" android:text="AppCompatCheckedTextView" android:textColor="@android:color/white" android:textSize="@dimen/buttonTextSize" /> <!-- android:checkMark="?android:attr/listChoiceIndicatorMultiple" --> <CheckedTextView android:id="@+id/p2Checkbox4" android:layout_width="match_parent" android:layout_height="@dimen/buttonHeight" android:background="@drawable/transparent_ripple" android:checkMark="@drawable/checkline" android:checked="true" android:gravity="left|center_vertical" android:text="CheckedTextView" android:textColor="@android:color/white" android:textSize="@dimen/buttonTextSize" /> <CheckBox android:id="@+id/p2Checkbox5" android:layout_width="match_parent" android:layout_height="@dimen/buttonHeight" android:background="@drawable/transparent_ripple" android:checked="true" android:gravity="center_vertical|end" android:text="CheckBox" android:textColor="@android:color/white" android:textSize="@dimen/buttonTextSize" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@android:color/white" /> <ToggleButton android:id="@+id/p2ToggleButton1" android:layout_width="match_parent" android:layout_height="@dimen/buttonHeight" android:background="@drawable/transparent_ripple" android:checked="true" android:drawableRight="@drawable/checkline" android:gravity="center_vertical|left" android:textAllCaps="false" android:textColor="@android:color/white" android:textOff="ToggleButtonOff" android:textOn="ToggleButtonOn" android:textSize="@dimen/buttonTextSize" /> <ToggleButton android:id="@+id/p2ToggleButton2" android:layout_width="match_parent" android:layout_height="@dimen/buttonHeight" android:background="@drawable/transparent_ripple" android:checked="true" android:drawableRight="@drawable/btn_check_material_anim" android:gravity="center_vertical|left" android:textAllCaps="false" android:textColor="@android:color/white" android:textOff="ToggleBtnnAnimOff" android:textOn="ToggleBtnnAnimOn" android:textSize="@dimen/buttonTextSize" /> 

示例checkline.xml(可绘制,请参阅drawable-v21中的animation版本链接)

示例transparent_ripple.xml(在drawable-v21中)

 <!-- Limit ripple to view object, can also use shape such as oval --> <item android:id="@android:id/mask" android:drawable="@android:color/white" /> <item> <selector xmlns:android="http://schemas.android.com/apk/res/android" android:enterFadeDuration="200" android:exitFadeDuration="200"> <item android:state_pressed="true"> <shape android:shape="rectangle"> <solid android:color="#80c0c000" /> </shape> </item> </selector> </item> 

示例transparent_ripple.xml(在drawable中,只突出显示没有可用的纹波

 <item android:state_pressed="true"> <shape android:shape="rectangle"> <solid android:color="#80c0c000" /> </shape> </item> <item> <shape android:shape="rectangle"> <solid android:color="@android:color/transparent" /> </shape> </item> 

如果不是必须使用CheckBox ,则可以使用Switch 。 开关在默认情况下在左侧显示文本。

 <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/location_permissions" android:textAppearance="@style/TextAppearance.AppCompat.Medium" android:textColor="@android:color/black" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <CheckBox android:id="@+id/location_permission_checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="8dp" android:onClick="onLocationPermissionClicked" /> </RelativeLayout> </LinearLayout>