我有一个可变宽度的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>


该独立代码段中也会出现问题:

<?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%,但是我认为问题在于它是多行文字。

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

我认为会发生以下情况:

视图试图将其放在一行上,然后扩展到最大宽度(假定不足以进行渲染),使其变为多行,但不会拉伸该视图。

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

希望这可以帮助。

10-02 03:02
查看更多