我有一个ListView,我想自定义列表中的每个项目的布局,以便它有一个按钮在右边,并有一个按钮的左侧有多个单词是TextViews(即左侧的每个TextView将是一个单词)。这里是我的意思图片: 重新对齐TextViews如果扩展过去的容器
现在我已经能够成功地得到ButtonImage我想要它(具有40dp的定义宽度)。如果我只是把TextView放在一个RelativeLayout中,我注意到它们堆叠在一起,所以我在每个TextView中相应地添加了“android:layout_toRightOf”。很显然,这意味着我永远不会有彼此之下的言辞。我已经在这个布局的左边设置了它的右边有40dp的填充(所以TextViews不会落在按钮后面或碰撞到它)。会发生什么情况是TextViews将完美排列在顶部,然后最后几个无法挤入的部分将垂直展开以填充左侧框。我想要设置左边框区域(使用TextViews),使得如果TextView要打到右侧墙壁(技术上来自父级右侧墙的40dp填充),它将一直向下移动到左边,所以这些词读得像一本书。还要注意,对于ListView中的任何给定项,左侧都会有1-5个TextView(单词)。我非常肯定,如果我能按自己想要的方式获得布局,那么我将永远不必担心左侧的堆叠高于按钮图像(无论如何这都是一个容易解决的问题)。这是我的代码:
<?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"
android:orientation="horizontal"
>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:paddingRight="40dp"
android:id="@+id/kwds"
>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/first"
/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/third"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/second"
/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fourth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/third"
/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fifth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/fourth"
/>
</RelativeLayout>
<ImageButton android:id="@+id/bkmbtn"
android:src="@drawable/bkbtn"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
>
</ImageButton>
</RelativeLayout>
在你们问之前,是的,真的需要有一个TextView每个单词(不幸)。如果有什么不清楚的地方,让我知道。并感谢您为我提供的任何帮助!我真的希望这是可能的。
感谢您的快速回复!不幸的是,它仍然不适用于您的修改。最后一个TextView仍然会按右边的按钮,然后垂直显示其字母,在ListView中拉伸项目。这是因为您已将包含TextView的LinearLayout的方向定义为“水平”。这就是为什么我想使用RelativeLayout。无论如何感谢的人,我永远不会忘记投票答案工作/提供良好的见解。我总是对任何帮助我的人表示感谢。 :-) – Vinay 2011-05-25 10:42:17