我有一个宽度可变的TextView,但最大宽度是250dp。但不知何故,Android总是将其设置为最大宽度。
使用较短的文本和没有最大宽度时,它可以工作,但如果文本足够长,字段就会超过屏幕宽度,它会与箭头重叠。
我已经尝试了这两个变体,它们给出了与图片相同的结果
<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" />
发布于 2015-02-06 21:13:27
不是100%确定,但我认为问题是它是一个多行文本。
当文本较短时,视图是否按预期运行?即"1.欢迎“
我认为会发生以下情况:
视图尝试将其放在一行上,然后扩展到其最大宽度,使其成为多行,但不拉伸视图。
你可以尝试使用“1.Willkomman bei\n nmeiner APP”,看看它是否显示了预期的行为。
希望这能有所帮助。
发布于 2015-02-06 19:53:25
使用maxWidth不会导致它伸展。
它很可能存在于您的代码中,用于:
android:layout_marginEnd="@dimen/defaultSpacing"
并检查任何维度:
style="@style/MarkerText"
因为单独设置maxWidth不会导致这种情况。
一定有一些图形在文本和右侧文本视图之间放置了填充,或者可能有一些特殊的页边距混乱。
使用maxWidth不应该强迫它向右移动,就像我在图像中展示的那样,我认为它是嵌套布局,布局和元素中有很多定义边距和填充的代码,这可能会迫使文本视图向右移动,文本会自动换行,因为它不能放在第一行中,但是文本视图本身仍然被推到右边。
https://stackoverflow.com/questions/28364965
复制相似问题