2015-02-06 96 views
1

嗨,我有一个可变宽度的Textview,但最大为250dp。但不知何故,Android将其设置为最大。Android TexxtView最大宽度

使用较短的文本和没有最大宽度的作品,但是文本足够长,字段超出屏幕宽度,它与箭头重叠。

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

附加infos

完整的XML

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

问题occours个罐独立片段,太

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

什么是style =“@ style/MarkerText”? – Andy 2015-02-06 11:51:31

+0

你不能缩小你的字体大小吗? – 2015-02-06 11:56:06

+0

@MSGadag的字体大小应该无关紧要。如果内容超过最大宽度,它应该仍然包装 – 2015-02-06 11:59:19

回答

0

使用maxWidth不会使其伸展。

这很可能会在你的代码:

android:layout_marginEnd="@dimen/defaultSpacing" 

和检查任何尺寸:

style="@style/MarkerText" 

由于单独设置maxWidth不会导致此问题。

必须有一些图形在您的文本和右侧文本视图之间填充填充,或者可能存在一些特殊的边距混乱。

使用maxWidth不应该像这样向右强制它,正如我在图像中显示的那样,我认为它是嵌套布局,并且在布局和元素中有很多定义边距和填充的编码,这可能会强制你的texview移动到右侧,文本会自动换行,因为它不适合第一行,但是textview本身仍然被推到右边。

image

+0

我认为问题可能在您的style.xml中;我不得不添加一个额外的词来强制第二行代码我坐在我的编辑器 – 2015-02-06 12:09:42

+0

我已添加更多信息 – wutzebaer 2015-02-06 12:38:27

+0

@wutzebaer我想知道为什么你的线性布局是宽度/高度wrapcontent和不匹配?垂直布局方向,还是在你的初始LinearLayout? – 2015-02-06 12:52:48

1

不是100%肯定,但我认为问题是,它是一个多行文本。

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

我觉得会发生什么事是这样的:

的观点试图使其在同一行,然后将其扩展,直到它的最大宽度,因为是不够的渲染,使得它多线,但不拉伸的观点。

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

希望这会有所帮助。

+0

textview应缩回以仅包围最宽线条的宽度。这不是原因。在我的答案中看到我的图像,它应该只包装文本。我认为他的嵌套布局,填充和边距有问题,特别是使用水平线性布局和负边距 – 2015-02-06 13:16:49

+0

我已经添加了一个独立的示例,它与封闭布局无关...... @shalafi我认为你假设是正确的,我添加了\ n它看起来不错,但文本是动态的,所以我无法预测在哪里添加换行符 – wutzebaer 2015-02-06 13:17:54