【问题标题】:Checkbox with multiline text带有多行文本的复选框
【发布时间】:2019-04-30 08:53:03
【问题描述】:

我想将顶部的checkbox 与两行文本对齐。我希望 复选框和文本在顶部的同一行

注意:我尝试过android:gravity="top",但在复选框的文本中添加了一些额外的填充。

我试过了:How to align CheckBox to the top of its description in Android,但没用。

我试过了:

<CheckBox
     android:id="@+id/chkConfirmSymbol"
     android:layout_width="0dp"
     android:layout_height="wrap_content"
     android:layout_marginLeft="@dimen/margin_10"
     android:layout_weight="1"
     android:button="@drawable/selector_checkbox_black"
     android:enabled="false"
     android:gravity="top"
     android:paddingTop="0dp"
     android:paddingLeft="@dimen/margin_10"
     android:text="@string/at_least_1_symbol"
     android:textColor="@drawable/selector_checkbox_text_black"
     android:textSize="@dimen/font_11" />

输出:

根据上面的图片,我在文本中得到了一些填充。

任何帮助将不胜感激。谢谢!

【问题讨论】:

  • 你想要的输出是什么?
  • 复选框和文本在同一行的顶部。你现在可以看到复选框太高了:(
  • @Annie 您能否将其添加到您的问题中,目前,它没有指定“复选框和文本在同一行的顶部。”
  • 您正在通过使用这些行 android:layout_width="0dp" android:layout_weight="1" 来限制编辑文本的宽度,因此在当前宽度下,文本将增加高度并且复选框圆圈将位于顶部。
  • @fatemehfallahiarezoudar 即使在删除重量和宽度后,它仍显示相同的输出

标签: android android-layout android-checkbox


【解决方案1】:

你不能用简单的方法吗?为什么需要android:layout_width="0dp"android:layout_weight="1"

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="top"
    android:text="At least 1 symbol(!@#$%^&*)"
    android:textSize="20sp" />

或者你可以看看this answer.它应该对你有帮助。

【讨论】:

  • 注意:如果用户在辅助功能设置中更改了他的字体大小,这将无法正常工作
  • 为此,请查看This answer.
  • 这个完全忽略了用户的偏好。 android:textSize="13dp".
  • 也许尝试使用 sp 而不是 dp 构建您的按钮会有所帮助(不确定没有尝试过)
【解决方案2】:

我刚刚给了一些android:paddingTop,你也可以尝试用同样的方法。

XML 代码(用于布局):

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <CheckBox
        android:id="@+id/cb_confirm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:enabled="false"
        android:paddingTop="5dp"
        android:paddingStart="10dp"
        android:paddingEnd="0dp"
        android:text="@string/at_least_1_symbol"
        android:textColor="@android:color/background_dark"
        android:textSize="12sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

</android.support.constraint.ConstraintLayout>

截图:

希望对你有帮助。

【讨论】:

    【解决方案3】:

    您可以在sdk &gt; platforms &gt; API_VERSION &gt; data &gt; res &gt; drawable中找到用于复选框的默认可绘制文件

    如果您检查与默认可绘制对象一起使用的任何资源图像,例如此可绘制对象中使用的btn_check_off,您可以看到默认图像在图像本身中有一些默认填充。您正在使用的自定义图像丢失了它。为您的图像资源添加默认填充,它应该可以正常工作。

    请参阅下图以供参考。

    【讨论】:

      【解决方案4】:

      这基本上就是你的做法:

      <CheckBox
          android:layout_width="match_parent"
          android:minLines="2"
      

      这仍然会在平板电脑(横向)屏幕上缩放为单行。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-09
        • 1970-01-01
        • 2016-10-07
        • 1970-01-01
        • 2021-05-20
        • 2012-05-27
        • 2011-05-26
        • 2017-07-08
        相关资源
        最近更新 更多