【问题标题】:wrap_content doesn't wrap contentwrap_content 不包装内容
【发布时间】:2019-12-06 02:24:32
【问题描述】:

我是 Android 编程的初学者。我正在创建一个应用程序,我想给文本加下划线。请查看我的代码 sn-p

<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:text="hello world"
        android:id="@+id/textview"
        android:layout_height="wrap_content"
         />
    <View
        android:layout_width="wrap_content"
        android:layout_height="1dp"
        android:background="@color/colorPrimary"/>
</LinearLayout>

实际上,我希望 textview 内容和 View 的宽度相同。但视图宽度大小与全屏宽度相同。

【问题讨论】:

  • 创建下划线注释掉 标记并将其添加到您的代码 textview.setPaintFlags(textview.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
  • 但我也想使用 View 和 In Xml 为 textview 加下划线
  • 然后为视图和文本视图设置一个固定大小。例如 300dp 而不是 wrap_content
  • 好的。为什么 wrap_content 不起作用?
  • 将视图的 xml 更改为:android:layout_width="match_parent"

标签: android textview android-linearlayout android-wrap-content


【解决方案1】:

如果您不指定宽度或高度,视图视图将始终全屏显示。 因此,您想使用CoordinatorLayoutRelativeLayout 以使View 宽度与TextView 相同。

【讨论】:

  • 我都指定了
  • View Android 上的wrap_content 将始终作为match_parent。
【解决方案2】:

在视图中为文本添加下划线您可以编辑如下所示的字符串,

 <resources>
    <string name="string_name">This is an <u>underline</u>.</string>
</resources>

并将其添加到您的文本视图中,例如,

<TextView
    android:layout_width="wrap_content"
    android:text="@string/string_name"
    android:id="@+id/textview"
    android:layout_height="wrap_content" />

希望对你有帮助!!

【讨论】:

    猜你喜欢
    • 2021-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-02
    • 1970-01-01
    相关资源
    最近更新 更多