2011-05-08 179 views
1

我真的很难想出一个可行的布局。我有一个填充屏幕宽度的视图。它包含三个子视图:Android布局空间填充问题

  1. 一些文本
  2. 正文后的数字在括号
  3. A键

按钮是右对齐,而文本项留如图所示:

 
| Some heading text (n)    [button] | 

问题是控制文本太长时会发生什么。我想这样,所以这个数字总是在主文本的右边可见。主文本应根据需要截断,以便其他两个视图保持可见。

 
| Some very very long headin... (n) [button] | 

最近我有它成功地截断的(N)正文结果总是被右对齐按钮旁边即使主文本是足够短,以适应。这不是我想要的。

你会如何处理这个问题?

我没有发布任何我当前的XML,以免损害任何人的建议。

+0

你使用什么底座布局? – Mannaz 2011-05-08 22:58:27

+0

我已经尝试了RelativeLayout和LinearLayout。 RelativeLayout对于外部容器来说似乎是最好的,可以将按钮固定在右边,我尝试在两个RelativeLayout或LinearLayout中嵌套两个文本视图。 – 2011-05-08 22:59:38

+0

你可以在这里发布你的xml吗? – ingsaurabh 2011-05-09 05:27:03

回答

1

我不相信有任何XML布局。我的猜测是,你将需要延长TextView测量内onDraw(...)文本长度,通过一些迭代相应地调整文本(即,在一个时间,直到文本适合在画布上移除一个字符)

我刚刚发现了另一个与您非常相似的问题:Ellipsize only a section in a TextView。没有其他答案,在中间椭圆大小。


另一个想法:

我不知道它是否会工作有一个的TextView与正文ellipsize左WRAP_CONTENT)和另一个与数圆括号中的wrap_content),两者都在水平线性布局内。该布局将的相对布局layout_toLeftOf按钮,这将是WRAP_CONTENTlayout_alignParentRight内部。

它有什么意义吗?我现在没有Eclipse来亲自测试它。不确定(n)文本视图是否会丢失在按钮后面或者没有长文本。

或者(和不感兴趣),你可以设置一个相对单一布局与两个textviews 所有layout_toRightOf和右对齐(layout_alignParentRight)按钮,并设置最大witdth OT第一的TextView( android:maxWidth)。 不过,您需要为不同的屏幕设置不同的布局。


具有固定最大宽度根据需要,将工作的一个例子:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="click me" 
    android:id="@+id/bt1" 
    android:layout_alignParentRight="true" 
    />  
    <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="short text" 
    android:lines="1" 
    android:ellipsize="end" 
    android:id="@+id/t1" 
    android:layout_alignParentLeft="true" 
    /> 
    <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="(n)" 
    android:lines="1" 
    android:id="@+id/n1" 
    android:layout_toRightOf="@id/t1" 
    /> 
    <Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="click me" 
    android:id="@+id/bt2" 
    android:layout_below="@id/bt1" 
    android:layout_alignParentRight="true" 
    />  
    <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="very long text that will not fit in any layout, regardless of the size of the screen" 
    android:lines="1" 
    android:ellipsize="end" 
    android:id="@+id/t2" 
    android:layout_below="@id/bt1" 
    android:layout_alignParentLeft="true" 
    android:maxWidth="220dp" 
    /> 
    <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="(n)" 
    android:lines="1" 
    android:id="@+id/n2" 
    android:layout_below="@id/bt1" 
    android:layout_toRightOf="@id/t2" 
    /> 
</RelativeLayout> 
+0

感谢您的所有建议。你的嵌套布局的想法并不完美:数字消失在按钮后面。 – 2011-05-09 20:31:23

+0

最终结果:我最终使用了android:ellipsize =“middle”,就像在其他问题中找到的一样。这并不完全令人满意,但它是我能找到的最佳折中方案。 – 2011-05-09 21:40:51

+0

@Graham回到家我正在玩Eclipse一点点。如果您可以使用最大宽度(例如200dp),那么您可以得到期望的结果。往上看。 – Aleadam 2011-05-09 23:59:38

0

尝试的LinearLayout,设置文本视图的重量为1 ,,和省略号设定为TruncateAt.MIDDLE。勾选此布局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="wrap_content"> 
    <TextView android:id="@+id/text" android:layout_width="0dp" 
     android:layout_height="wrap_content" android:lines="1" 
     android:ellipsize="middle" android:gravity="left" 
     android:layout_weight="1" /> 
    <Button android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 
0

的关键是项目的排序,因为这是他们所测量的顺序,以确保您的按钮和(n)文本获得的总体布局足够的空间:

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    > 

    <Button 
    android:id="@+id/button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    /> 

    <TextView 
    android:id="@+id/middle" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="left" 
    android:layout_toLeftOf="@id/button" 
    android:text="(n)" 
    /> 

    <TextView 
    android:id="@+id/title" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_toLeftOf="@id/middle" 
    android:layout_alignParentLeft="true" 
    android:gravity="left" 
    android:ellipsize="end" 
    android:text="Some really long to text to make this flow over" 
    android:lines="1" 
    android:maxLines="1" 
    /> 

</RelativeLayout> 
+0

谢谢。这受到我在问题中提到的问题的影响:当文本太长时很好,但是当文本很短时,中间视图固定在右边(按钮旁边),当它紧邻主要TextView中。 – 2011-05-09 19:42:56