【问题标题】:Android: How to align the CheckBox text in the bottom?Android:如何对齐底部的复选框文本?
【发布时间】:2014-11-20 02:21:43
【问题描述】:

基本上我想要的是一个带有图标的复选框以及图标下方的文本,如下所示:

 -------------------
|   CheckBox Icon   |
|                   |
|   CheckBox Text   |
|                   |
 -------------------

默认情况下,图标位于左侧,文本位于右侧,两者处于同一级别。在我正在使用的布局下方:

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Switch state"
    android:button="@drawable/my_custom_selector" />

关于如何实现将图标放在顶部并将文本放在图标下方的任何想法?在此先感谢:)

【问题讨论】:

  • 您能从您的布局中发布您的 XML 吗?
  • @IAmTheSquidward 请检查我的更新;)
  • 我遇到了同样的问题,我用this解决了它

标签: android android-layout android-view android-checkbox


【解决方案1】:

我遇到了同样的问题,我已经用这个解决了:

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

【讨论】:

    【解决方案2】:

    您可以使LinearLayout 垂直并将CheckBox(不带文本)放在顶部,将文本放在下面的TextView 中。

    最后创建 LinearLayout clickable 并以编程方式选中/取消选中。

    【讨论】:

      【解决方案3】:

      CheckedTextView + 带有选中和未选中可绘制对象的状态列表:

      <CheckedTextView
          android:drawableTop="@drawable/checkmark"
          android:checked="true"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Checked text"/>
      

      @drawable/复选标记

      <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:drawable="@drawable/unchecked" android:state_checked="false"></item>
      <item android:drawable="@drawable/checked"></item></selector>
      

      (checkmark.xml 被简化)。

      【讨论】:

        【解决方案4】:

        上述解决方案对我不起作用。缺少的是其中的 Kotlin/Java 代码。

        所以将它添加到你的 Kotlin 代码中

        cb_twitter.setOnClickListener({
                    (it as CheckedTextView).toggle()
                })
        

        这是我的 XML 代码:

        <CheckedTextView
            android:id="@+id/cb_twitter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:checked="false"
            android:drawableTop="@drawable/checkbox_selector_twitter"
            android:gravity="center_horizontal"
            android:text="@string/Twitter"
            android:textAlignment="gravity"/>
        

        这里我也将文本设置为居中

        android:gravity="center_horizontal"
        android:textAlignment="gravity"
        

        【讨论】:

          【解决方案5】:

          我只是通过使用线性布局(垂直)来实现的,因为我没有使用任何自定义复选框 xml

          我的情况就像一个文本在一个复选框的顶部

           <androidx.appcompat.widget.LinearLayoutCompat
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:orientation="vertical"
                          >
                          <androidx.appcompat.widget.AppCompatTextView
                              android:layout_width="15dp"
                              android:layout_height="15dp"
                              android:text="text1"
                              android:layout_gravity="center"
                              android:textSize="15dp"/>
                          <CheckBox android:id="@+id/textcheckbox"
                              android:layout_width="wrap_content"
                              android:layout_height="wrap_content"
                              android:onClick="onCheckboxClicked"/>
                      </androidx.appcompat.widget.LinearLayoutCompat>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-08-17
            • 1970-01-01
            • 2013-01-11
            • 2013-07-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多