我想出一个有效的布局真的很困难。我有一个填充屏幕宽度的视图。它包含三个子视图:
- 一些文本
- 正文后面括号中的数字
- 一个按钮
按钮右对齐,文本项一个接一个地左对齐,如下所示:
|一些标题文本 (n) [按钮] |
问题在于控制文本太长时会发生什么。我想要这样,以便数字始终在正文的右侧可见。如果需要,应截断正文,以便其他两个视图保持可见。
|一些非常非常长的头...(n) [按钮] |
我得到的最接近的,它成功地截断了正文,导致 (n) 始终在按钮旁边右对齐,即使正文足够短以适合。那不是我想要的。
你会如何处理这个问题?
我还没有发布我当前的任何XML,以免它损害任何人的建议。
我不相信有任何xml布局。我的猜测是,您需要扩展TextView
并测量onDraw(...)
内的文本长度,通过一些迭代相应地调整文本(即一次删除一个字符,直到文本适合画布)
我刚刚发现了另一个与您的问题非常相似的问题:仅椭圆化文本视图中的一个部分。除了中间的椭圆形之外没有其他答案。
另一个想法:
我想知道在水平线性布局中,有一个带有正文(左省略号,wrap_content)的文本视图和另一个带有括号中的数字 (wrap_content) 的文本视图是否有效。该布局将位于相对布局内,并layout_toLeftOf按钮,该按钮将wrap_content,layout_alignParentRight。
这有什么意义吗?我现在没有 Eclipse 来自己测试它。不确定 (n) 文本视图是否会在按钮后面丢失,或者不会使用长文本。
或者(不太有趣),您可以设置一个相对布局,两个文本视图全部layout_toRightOf,按钮向右对齐(layout_alignParentRight),并设置第一个文本视图的最大宽度(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,并将省略号设置为 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>
关键是项目的顺序,因为这是它们的测量顺序,以确保您的按钮和(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>