2011-02-10 101 views
5

我试图让文本行,这将是这样的Android的布局使用layout_weight,layout_width和maxWidth

foofoofoo - barbarbar

,但我希望它椭圆foo和bar,如果它赢得了” t适合一条线。 即我试图缩短它们。

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <TextView android:id="@+id/text_1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:adjustViewBounds="true" 
     android:maxWidth="0dip" 
     android:layout_weight="1" 
     android:textColor="#ffffff" 
     android:singleLine="true" 
     android:ellipsize="true" 
     android:text="foofoofoofoo" /> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="#ffffff" 
     android:text=" - " /> 
    <TextView 
     android:id="@+id/text_2" 
     android:layout_width="wrap_content"       
     android:layout_height="wrap_content" 
     android:maxWidth="0dip" 
     android:layout_weight="1" 
     android:textColor="#ffffff" 
     android:singleLine="true" 
     android:ellipsize="true" 
     android:text="barbarbarbar" /> 
</LinearLayout> 

熊与我这部分工作。

设置TextViewlayout_width0diplayout_weight1意味着它们会占用每个可用空间的50%。

设置singleLinetrueellipsizetrue意味着他们会像foofoo ...如果文本比容器大。

因此,我的结果(如果文字是更长的时间)应该是

foofoo .. - BARBAR ..

它是!所以这个工作。

现在我试图解决的情况下,如果第一TextView(ID:文本1)具有文本小于由layout_width="0dip"layout_weight="1"我希望它wrap_content给出的50%。否则,它看起来像:

空白- BARBAR ..

,我想

富 - barbarbar

这就是为什么我更改了layout_width="wrap_content"并添加了android:maxWidth="0dip"但这不起作用!它似乎无视我说wrap_content,仍然给这个观点50%!

我读到,你需要添加android:adjustViewBounds="true"maxWidth工作,但这没有任何影响。

回答

10

这是我能做的最好的,尝试更改字符串以查看它是否按预期工作。

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:shrinkColumns="0,2" 
    android:stretchColumns="0,2"> 
    <TableRow> 
     <TextView android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:singleLine="true" 
      android:ellipsize="end" 
      android:text="foofoofoo"/> 
     <TextView android:id="@+id/textView3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="-" 
      android:gravity="center_horizontal"/> 
     <TextView android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:singleLine="true" 
      android:ellipsize="end" 
      android:text="barbarbarbarbarbarbarbarbarbarbarbar"/> 
    </TableRow> 
</TableLayout> 
2

很难确切地告诉你想要什么,但我相信答案是你不能这样做,除非你动态调整你的代码布局。原因是因为你要求系统以多种不同的方式工作。

首先,当视图中的文本数量相同时,您希望视图平均分享空间。

其次,您希望的视图不是当另一视图中的文本较少时,可以平等地分享空间。

这里有多种其他情况可以任何你想要的方式处理。然而,我想传达的是,你要求相同的布局以不同的方式处理事情,这就是为什么你无法通过单一布局实现这一点。

+0

我想基本上说maxWidth是共享空间(因此elipsing),但通常只是wrap_content。所以我会假设它会wrap_content,除非wrap_content的dip值> 50%,那么它会使用maxWidth(这是50%),不是? – Blundell 2011-02-10 20:25:29

+0

`除非wrap_content的dip值> 50%,那么它会使用maxWidth` ...我真的不知道你想说什么。 `wrap_content`不使用dip值。 – user432209 2011-02-10 23:38:38

相关问题