如何在 Android ListView 中设置所选项目的样式?

发布于 2024-09-29 07:33:30 字数 1695 浏览 1 评论 0原文

我是一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心是晴朗的。 2024-10-06 07:33:30

我这样做:

 <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"

在我的例子中,单元格背景只会是白色的,按下时会变成灰色。

我希望它有帮助!

I do this :

 <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>

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!

娇纵 2024-10-06 07:33:30

您必须使用 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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文