具有最大宽度的 Android TextView

标签 android textview

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

对于较短的文本和没有最大宽度的情况,它可以工作,但是文本足够长以至于字段超过屏幕宽度,它与箭头重叠。

enter image description here

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

<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" />

我希望它看起来像这样:

enter image description here

附加信息:

完整的 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>

问题也出现在这个独立的 fragment 中:

<?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" />

最佳答案

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

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

我认为会发生以下情况:

View 试图使其在一行上,然后扩展到其最大宽度,假设这不足以进行渲染,使其成为多行,但不会拉伸(stretch) View 。

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

希望这对您有所帮助。

关于具有最大宽度的 Android TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28364965/

相关文章:

android - 在 Android 的 textview 中格式化文本项目符号的最简单方法

java - 正则表达式:在任何情况下突出显示 TextView 中的输入

android - 错误 : Conflict with dependency 'com.android.support:multidex' in project

Android开发——SQLite存储 float

java - findViewById 出现问题,用图表解释

android - 折叠工具栏布局 : setShadowLayer

Android - OnItemClickListener 不捕获适配器 View 中的事件点击

安卓 WebView : Download when content-type is application/pdf and render web page if not

android - 使用 URL 用屏幕宽度的图像填充 TextView

android - 如何在android中按主题级联样式