如何在 Android ListView 中设置所选项目的样式? [英] How do I style selected item in Android ListView?

查看:32
本文介绍了如何在 Android ListView 中设置所选项目的样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名 Android 新手,正在尝试学习 UI 方面的东西,我正在努力学习.这就是我现在所拥有的:

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

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

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

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.

非常感谢,D.

推荐答案

我这样做:

 <selector xmlns:android="http://schemas.android.com/apk/res/android">

   <item android:state_pressed="true">
       <shape  >
         <solid android:color="#929292" />
       </shape>
   </item>



   <item>
     <shape  >
        <solid android:color="#FFFFFF" />
     </shape>
   </item>

 </selector>

所以你可以使用这个属性android:state_pressed="true"

So you can use this property android:state_pressed="true"

在我的示例中,单元格背景在按下时只会是白色和灰色.

In my example, the cell background will only be white, and grey when pressed.

希望能帮到你!

这篇关于如何在 Android ListView 中设置所选项目的样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆