【问题标题】:How can I align checkbox with text to right side of the screen?如何将复选框与屏幕右侧的文本对齐?
【发布时间】:2016-12-07 16:34:02
【问题描述】:

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

这里是引用复选框的xml的sn-p:

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

如何将复选框及其上的文本对齐到屏幕右侧?

提前致谢!

【问题讨论】:

  • 你的意思是复选框文本向右移动但框不移动??
  • @Andolaso​​ft 是的,就是这样。文本正在向屏幕右侧移动,但框仍在屏幕左侧。
  • stackoverflow.com/questions/3156781/….. 这对你有帮助吗..
  • 父布局表布局包含哪些?

标签: android checkbox


【解决方案1】:

试试这个

    <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" 和 设置这个可绘制“?android:attr/listChoiceIndicatorMultiple” 任意drawableRight或drawableLeft。

【讨论】:

    【解决方案2】:

    只需添加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"/>
    

    它将在左侧显示text,在右侧显示check box

    【讨论】:

    • 如果您也能解释一下这段代码的作用会有所帮助吗? android:button="@null"
    【解决方案3】:

    属性android:gravity 正在处理视图本身,而android:layout_gravity 是建议父视图对齐它的子视图。

    如果您想告诉父布局将其子布局向左或向右对齐,您有两种选择:

    1. 在父级上使用android:gravity,因此父级将尝试在该重力下对齐所有子视图。
    2. android:layout_gravity 在子视图上,并且只有此视图会尝试将自身与该重力对齐。

    【讨论】:

    • 非常感谢!你的解释是唯一为我工作的人。除了你的答案,我不得不删除android:layout_weight="1"
    【解决方案4】:

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

    【讨论】:

      【解决方案5】:

      试试这个。您将在右侧获得文本和复选框

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-02-03
        • 1970-01-01
        • 2012-10-30
        • 2016-08-08
        • 2020-10-17
        • 1970-01-01
        • 2015-07-05
        • 1970-01-01
        相关资源
        最近更新 更多