【问题标题】:Android TextView with maxwidth具有最大宽度的 Android TextView
【发布时间】:2015-04-06 13:08:49
【问题描述】:

我有一个可变宽度的 TextView,但最大值为 250dp。但不知何故,Android 总是将其设置为 maxwidth。

使用较短的文本并且没有 maxwidth 它可以工作,但是文本足够长以至于字段超过屏幕宽度,它会与箭头重叠。

我已经尝试过这两种变体,结果与图片中的相同

<TextView
    android:id="@+id/bubbleText"
    style="@style/MarkerText"
    android:maxWidth="250dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FF0000"
    android:layout_marginEnd="@dimen/defaultSpacing" />

<TextView
    android:id="@+id/bubbleText"
    style="@style/MarkerText"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="@dimen/defaultSpacing"
    android:layout_weight="1"
    android:background="#FF0000"
    android:maxWidth="250dp"
    android:singleLine="false" />

我希望它看起来像这样:

其他信息:

完整的 XML

<LinearLayout
    android:id="@+id/markerTextBubble"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical"
    android:visibility="invisible" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/marker_text_background" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingBottom="@dimen/tinySpacing"
            android:paddingEnd="@dimen/defaultSpacing"
            android:paddingStart="@dimen/defaultSpacing"
            android:paddingTop="@dimen/tinySpacing" >

            <TextView
                android:id="@+id/bubbleText"
                style="@style/MarkerText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="@dimen/defaultSpacing"
                android:background="#FF0000"
                android:maxWidth="250dp" />

            <mypackage.IconTextView
                xmlns:android="http://schemas.android.com/apk/res/android"
                style="@style/MarkerTextIcon"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="@string/icon_map_arrow" />
        </LinearLayout>
    </LinearLayout>

    <ImageView
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_marginBottom="-5dp"
        android:layout_marginTop="-1dp"
        android:src="@drawable/marker_text_background_rectangle" />
</LinearLayout>
<style name="MarkerText" parent="android:Widget.Holo.Light.TextView">
    <item name="android:textColor">@color/myWhite</item>
    <item name="android:textSize">@dimen/markerTextSize</item>
</style>

<dimen name="markerTextSize">22sp</dimen>

这个独立的sn-p也出现问题:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FF0000"
    android:maxWidth="250dp"
    android:text="1. Willkommen bei meiner APP"
    android:textSize="22sp" />

【问题讨论】:

  • 什么是 style="@style/MarkerText" ??
  • 你不能减小你的字体大小吗?
  • @user3956566 如果内容超过最大宽度,它怎么会换行?
  • @user3956566 是那个beimeiner一个字吗?
  • 请张贴想要的图片...

标签: android textview


【解决方案1】:

不是 100% 确定,但我认为问题在于它是多行文本。

当文本较短时,视图的行为是否符合预期?即“1. 欢迎”

我认为会发生以下情况:

视图尝试使其成为一条线,然后扩展至其最大宽度,因为这不足以进行渲染,使其成为多线,但不拉伸视图。

您可以尝试使用“1. Willkommen bei\nmeiner APP”来查看它是否显示了预期的行为。

希望这会有所帮助。

【讨论】:

  • 我添加了一个独立的示例,它与封闭布局无关...@shalafi 我认为你的假设是正确的,当我添加一个 \n 它看起来不错,但是文本是动态的,所以我无法预测在哪里添加换行符
【解决方案2】:

使用 maxWidth 不会导致它拉伸。

它很可能在您的代码中:

android:layout_marginEnd="@dimen/defaultSpacing" 

并检查任何尺寸:

style="@style/MarkerText"

因为单独设置 maxWidth 不会导致这种情况。

一定有一些图形在你的文本和右侧文本视图之间放置了填充,或者可能是一些特殊的边距混乱。

使用 maxWidth 不应该像我在图片中向您展示的那样将其强制向右移动,我认为这是嵌套布局,并且在您的布局和元素中有很多定义边距和填充的编码,这可能是强制您的 texview 向右移动,文本将自动换行,因为它无法放入第一行,但 textview 本身仍被推到右侧。

【讨论】:

  • 我添加了更多信息
  • 因为它只是一个小气泡,显示在地图上,而不是全屏
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多