Android布局空间填充问题

时间:2011-05-08 22:56:21

标签: android layout truncate space fill

我很难想出一个有效的布局。我有一个填充屏幕宽度的视图。它包含三个子视图:

  1. 一些文字
  2. 正文后的括号中的数字
  3. 按钮
  4. 按钮右对齐,文本项一个接一个地左对齐,如图所示:

    | Some heading text (n)             [button]  |
    

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

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

    我最接近成功截断主文本的结果是(n)总是在按钮旁边右对齐,即使主文本足够短以适应。那不是我想要的。

    你会怎么做?

    我还没有发布我现有的任何XML,以免影响任何人的建议。

3 个答案:

答案 0 :(得分:1)

我不相信有任何xml布局。我的猜测是你需要扩展TextView 衡量onDraw(...) 中的文字长度,通过一些迭代相应地调整文本(即一次删除一个字符,直到文本适合画布)

我刚发现另一个与你的问题非常相似的问题:Ellipsize only a section in a TextView。没有其他答案比中间的椭圆形。


另一个想法:

我想知道是否可以使用一个主文本 ellipsize left wrap_content )和括号中的数字 wrap_content ),都位于水平线性布局内。该布局将在内部相对布局 layout_toLeftOf 按钮,即 wrap_content layout_alignParentRight

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

或者(并且不那么有趣),您可以使用两个文本视图 all layout_toRightOf 设置一个相对布局,并将按钮对齐到右侧( layout_alignParentRight < / strong>)并设置第一个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>

答案 1 :(得分: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>

答案 2 :(得分: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>