使用RelativeLayout在ListView中重复RatingBar的怪异
以下是 ListView 中一行的一些 xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:padding="6dip">
<ImageView android:id="@+id/cover" android:layout_width="64dip"
android:layout_height="64dip" android:layout_alignParentTop="true"
android:layout_alignParentBottom="true" android:layout_marginRight="6dip" />
<TextView android:id="@+id/loan" style="@style/DetailsLabel.Small"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_toRightOf="@id/cover" android:layout_alignParentRight="true" />
<TextView android:id="@+id/title" style="@style/DetailsLabel.Small"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_toRightOf="@id/cover" android:layout_alignParentRight="true"
android:layout_alignParentTop="true" android:layout_above="@id/loan"
android:layout_alignWithParentIfMissing="true" android:gravity="center_vertical"
android:singleLine="true" android:ellipsize="end" />
<RatingBar android:id="@+id/ratingBarIndicator" style="?android:attr/ratingBarStyleSmall"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_toRightOf="@id/cover" android:layout_below="@id/title"
android:layout_alignParentRight="true" android:gravity="center_vertical" />
这是结果:
(这是一个普通的 Donut 1.6)。
我想要实现的是左侧的封面,右侧是标题、评级栏和可选的贷款状态。正如您所看到的,RatingBar 不断重复(它应该只有五颗星);每当设置了贷款状态时,标题就会消失。如何修复我的 XML 以达到我想要的效果?
Here is some xml for a row in a ListView:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:padding="6dip">
<ImageView android:id="@+id/cover" android:layout_width="64dip"
android:layout_height="64dip" android:layout_alignParentTop="true"
android:layout_alignParentBottom="true" android:layout_marginRight="6dip" />
<TextView android:id="@+id/loan" style="@style/DetailsLabel.Small"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_toRightOf="@id/cover" android:layout_alignParentRight="true" />
<TextView android:id="@+id/title" style="@style/DetailsLabel.Small"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_toRightOf="@id/cover" android:layout_alignParentRight="true"
android:layout_alignParentTop="true" android:layout_above="@id/loan"
android:layout_alignWithParentIfMissing="true" android:gravity="center_vertical"
android:singleLine="true" android:ellipsize="end" />
<RatingBar android:id="@+id/ratingBarIndicator" style="?android:attr/ratingBarStyleSmall"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_toRightOf="@id/cover" android:layout_below="@id/title"
android:layout_alignParentRight="true" android:gravity="center_vertical" />
And here is the result:
(That's a plain Donut 1.6).
What I'm trying to achieve is a cover on the left, followed by a title, rating bar, and optional loan status on the right. As you can see the RatingBar is repeating endlessly (it should only have five stars); and whenever there IS a loan status set, the title disappears. How can I fix my XML to achieve the effect I want?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将从修复评级栏开始,如下所示:
然后将其与其父级的右侧对齐(可选地使用 android:layout_centerVertical="true" )。
之后,将您的标题放置在封面右侧和评级栏左侧)
然后,您可以将借用标签放置在标题下方和封面右侧。
I'd start with fixing rating bar as following:
Then align it to the right of its parent ( optionally with android:layout_centerVertical="true" ).
After that position your title to the right of cover and to the left of rating bar )
And then you can have you Loaned label to be, for example, below title and to the right of cover.
您可以在 Heirarchy Viewer(位于 SDK 的工具文件夹中)中查看您的布局,以了解出了什么问题。
就个人而言,为了实现该布局,我会使用一些 LinearLayout 而不是relativelayout。我发现RelativeLayouts 非常令人困惑并且很难正确理解(但这只是我的观点)。
例如:
如果您不希望贷款文本视图在空时占用任何空间,则可能需要在 ListView 适配器的 getView 中将其可见性设置为 GONE。不过还是尝试一下吧。
You might take a look at your layout in Heirarchy Viewer (found in the tools folder of the SDK) to get an idea of what is going wrong.
Personally, to achive that layout I would use a few LinearLayouts instead of a RelativeLayout. I find RelativeLayouts to be very confusing and hard to get right (but that is just my opinion).
For example:
If you don't want the loan textView to take up any space when it is empty, you may need to set it's visibility to GONE in getView of your ListView adapter. Try it out though.