如何将复选框和文本对齐到屏幕右侧?

3

我在TableRow内有一个复选框。我想将复选框和文本都对齐到屏幕的右侧,但当我使用android:gravity="right"时,它只会将文本对齐到屏幕的右侧,而不是方框(复选框本身)。看起来它们是分别对齐的(复选框在左侧,文本在屏幕右侧)。

这里是与复选框相关的xml片段:

<TableRow android:layout_height="wrap_content"
          android:layout_width="match_parent"
          android:layout_marginBottom="10dp">

          <CheckBox
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Select"
                android:id="@+id/checkboxSelect"
                android:layout_column="0"
                android:layout_weight="1"
                android:gravity="right"/>
</TableRow>

我该如何将复选框和上面的文本都靠右对齐?

提前感谢你!


你的意思是复选框文本向右移动,但框没有移动吗? - Andolasoft Inc
@Andolasoft 是的,就是这样。文本向屏幕右侧移动,但框仍在屏幕左侧。 - Francisco Romero
这是一个关于Android复选框如何显示在右侧的问题。希望这能对你有所帮助。 - Andolasoft Inc
哪个父布局包含表格布局? - Piyush
5个回答

8

只需按照以下方式添加Checkbox

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

它会在左边显示“文本”,在右边显示“复选框”。

如果您能解释一下这段代码的作用,那会很有帮助。android:button="@null" - hsm59

2

属性android:gravity作用于视图本身,而android:layout_gravity是建议父视图如何对齐其子视图。

如果您想要告诉父布局将其子项对齐到左侧或右侧,则有两个选项:

  1. 在父视图上使用android:gravity,因此父视图将尝试将所有子视图对齐到该位置。
  2. 在子视图上使用android:layout_gravity,只有这个视图会尝试以该重力方式对齐自己。

1
非常感谢!你的解释是唯一对我有用的。除了你的答案,我还必须删除 android:layout_weight="1" - Francisco Romero

0
尝试这个。您将在右侧获得文本和复选框。
<TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_marginBottom="10dp"
        android:gravity="right">

        <CheckBox
            android:id="@+id/checkboxSelect"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:gravity="right|center_vertical"
            android:text="Select" />
    </TableRow>

0

尝试使用RelativeLayout

<RelativeLayout 
      android:layout_height="wrap_content"
      android:layout_width="match_parent"
      android:layout_marginBottom="10dp">

      <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@id/text_view"
            android:id="@+id/checkboxSelect"/>

      <TextView 
            android:id="@+id/text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"/>
</RelativeLayout>

0

试试这个

    <CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center|end"
    android:layout_gravity="start"
    android:button="@null"
    android:drawableRight="?android:attr/listChoiceIndicatorMultiple"
    android:text="hello" />

设置android:button="@null",以及将这个drawable "?android:attr/listChoiceIndicatorMultiple" 设置为drawableRight或者是drawableLeft,根据你自己的喜好来决定。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接