【问题标题】:How to align CheckBox to the top of its description in Android如何将 CheckBox 与 Android 中的描述顶部对齐
【发布时间】:2019-03-02 09:24:02
【问题描述】:

我想将CheckBox“符号”与它的描述顶部对齐。

我现在拥有的:

我想要的:

当前xml:

<CheckBox android:id="@+id/register_checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/register_hint"/>

我试图通过操纵layout_gravity 属性来实现这一点,但它不起作用。

【问题讨论】:

  • 也许并不优雅,但是放置一个没有文本的复选框,然后在它旁边放置一个 textview 可能会做你想要的。因为我不知道如何在本地做你想做的事情。

标签: android android-layout checkbox android-xml


【解决方案1】:

android:gravity="top" 将解决问题:

<CheckBox android:id="@+id/register_checkbox"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/register_hint"
          android:gravity="top"/>

请注意,android:gravityandroid:layout_gravity 不同。

【讨论】:

  • 您的解决方案有效!事实上,我正在操纵android:layout_gravity 属性,所以这就是我看不到任何效果的原因。
  • 为什么我的不工作?我必须在左侧使用 CheckBox,在右侧使用 TextView。
  • 这在 CJK 等某些语言上无法正常工作
【解决方案2】:
  • 使用

    android:gravity="top" 它会将盒子固定在顶部

  • 使用

    padding_top="4dp" 将仅调整文本填充(不从顶部更改框填充)

【讨论】:

  • 添加 padding_top 解决了文本顶部难看的剪切问题。谢谢!
【解决方案3】:

我发现:

 android:gravity="fill"

比“顶部”效果更好。如果复选框也可能仅显示为一行,则顶部看起来不正确。

【讨论】:

  • 其实这似乎是完美的解决方案!它同样适用于短(单行)文本和长文本,并且无需使用静态填充对字体大小进行假设。
【解决方案4】:

你可以试试这个代码:-

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false">
        <layer-list>
            <item android:top="2dp">
                <bitmap android:gravity="center" android:src="@drawable/checkbox" />
            </item>
        </layer-list>
    </item>
    <item android:state_checked="true">
        <layer-list>
            <item android:top="2dp">
                <bitmap android:gravity="center" android:src="@drawable/checkboxselected" />
            </item>
        </layer-list>
    </item>
    <item>
        <layer-list>
            <item android:top="2dp">
                <bitmap android:gravity="center" android:src="@drawable/checkbox" />
            </item>
        </layer-list>
    </item>
</selector>

【讨论】:

  • 如果你有矢量可绘制使用 而不是
猜你喜欢
  • 1970-01-01
  • 2012-05-03
  • 2020-12-17
  • 1970-01-01
  • 2011-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多