如何在 Android ListView 中设置所选项目的样式?
我是一个 Android 新手,试图学习 UI 方面的东西,它正在做我的头脑。这就是我现在所拥有的:
breeds_listing.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<ListView android:id="@+id/breedsListView"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:dividerHeight="0px" android:divider="#00000000"></ListView>
</LinearLayout>
Breeds_listing_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="@drawable/list_item_background"
android:orientation="horizontal" >
<TextView android:id="@+id/allbreeds_single_item"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:gravity="center_horizontal"></TextView>
</LinearLayout>
list_item_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#043402" />
<stroke android:width="3dp" color="#ffff8080" />
<corners android:radius="8dp" />
<padding android:left="10dp" android:top="10dp" android:right="10dp"
android:bottom="10dp" />
</shape>
我正在努力弄清楚如何设计所选的样式物品?目前,所选项目具有可怕的橙色背景,在绿色圆角矩形下方,给出橙色轮廓效果。我确信我没有按照最佳实践做事,因此任何建议或改进将不胜感激。
非常感谢, D .
I am an Android newbie trying to learn the UI side of things and it's doing my head in. Here's what I have right now:
breeds_listing.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<ListView android:id="@+id/breedsListView"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:dividerHeight="0px" android:divider="#00000000"></ListView>
</LinearLayout>
breeds_listing_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="@drawable/list_item_background"
android:orientation="horizontal" >
<TextView android:id="@+id/allbreeds_single_item"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:gravity="center_horizontal"></TextView>
</LinearLayout>
list_item_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#043402" />
<stroke android:width="3dp" color="#ffff8080" />
<corners android:radius="8dp" />
<padding android:left="10dp" android:top="10dp" android:right="10dp"
android:bottom="10dp" />
</shape>
What I am struggling to figure out is how do I style the selected item? At the moment the selected item has a ghastly orange background which, under the rounded green rectangle, gives an orange outline effect. I'm sure I have not done things in line with best practices so any suggestions or improvements would be greatly appreciated.
Many thanks,
D.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我这样做:
所以你可以使用这个属性
android:state_pressed="true"
在我的例子中,单元格背景只会是白色的,按下时会变成灰色。
我希望它有帮助!
I do this :
So you can use this property
android:state_pressed="true"
In my example, the cell background will only be white, and grey when pressed.
I hope it helps!
您必须使用 StateListDrawable。
您可以在此处找到 ListView 项目的示例。
如果您希望在选择项目时具有某种颜色/可绘制内容,请使用
android:state_focused="true"
。You have to use a StateListDrawable.
You can find an example for ListView items here.
If you want a certain color/drawable when the item is selected, use
android:state_focused="true"
.