Android 如何在单击时突出显示视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3263169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:21:09  来源:igfitidea点击:

Android how to make View highlight when clicked?

androidxmllistviewlayoutuser-interface

提问by Kman

I have a linear layout in which each row is inflated programatically and I want the rows to behave like the ListView when clicked on. That is, I want the row to highlight in the exact same way/colour that the default ListView does. How would I go about doing this?

我有一个线性布局,其中每一行都以编程方式膨胀,我希望这些行在单击时表现得像 ListView。也就是说,我希望该行以与默认 ListView 完全相同的方式/颜色突出显示。我该怎么做呢?

回答by Kman

Ok I have finally figured out how to do this...basically it is done using a selector like the color selector linked by style except instead of 'color' use a drawable for the states and you can refer to the default list drawable that is used in ListView by this:

好的,我终于想出了如何做到这一点......基本上它是使用一个选择器完成的,比如按样式链接的颜色选择器,除了“颜色”使用状态的可绘制对象之外,您可以参考默认的可绘制列表通过以下方式在 ListView 中使用:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
    android:drawable="@android:drawable/list_selector_background" />      
</selector>

and using this xml as the background for my View.

并使用此 xml 作为我的视图的背景。

All the public default drawables can be found here: http://developer.android.com/reference/android/R.drawable.html

所有公共默认可绘制对象都可以在这里找到:http: //developer.android.com/reference/android/R.drawable.html

回答by davenpcj

I was able to do the same with a text view that I wanted to behave like a list item by using:

我能够使用文本视图执行相同的操作,我想通过使用以下命令使其表现得像列表项:

<Textview
....
android:background="@android:drawable/list_selector_background"
/>

回答by Dany

if you still have a problem with that then please remember that some of UI elements are not clickable (RelativeLayout), so you have to add one more line:

如果您对此仍有问题,请记住某些 UI 元素不可点击(RelativeLayout),因此您必须再添加一行:

<RelativeLayout 
    ....
    android:clickable="true"
    ...

回答by st0le

Thismight be a good place to start looking.

可能是开始寻找的好地方。

Although, i would advise you to use the ListView itself, rather than implementing it again.

虽然,我建议您使用 ListView 本身,而不是再次实现它。

回答by SAndroidD

To your listview set property

到您的列表视图设置属性

android:listSelector="@color/test"

and this test color set any transparent color you like. you can create any transparent color by using hex transparent color

并且此测试颜色设置您喜欢的任何透明颜色。您可以使用十六进制透明颜色创建任何透明颜色