单击时如何突出显示TextView或LinearLayout?

时间:2010-02-02 00:21:31

标签: android

我有一个包含三个TextView的LinearLayout。我想在用户点击TextView时突出显示TextView或整个布局。他们是否有办法实现这一目标?

感谢。

3 个答案:

答案 0 :(得分:14)

我supperpose 2 Layout

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"

    >

    <LinearLayout
        android:id="@+id/root"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="?android:attr/selectableItemBackground"
        android:clickable="true"
        >
...
    </LinearLayout>
</LinearLayout>

findViewById(R.id.root).setOnClickListener(...);

答案 1 :(得分:7)

我无法让罗曼的方法奏效,但我找到了答案here。使用XML资源作为背景drawable和瞧!它就像一个魅力。

答案 2 :(得分:3)

有很多方法可以做到这一点。

最简单的方法是使用各种android:focusable属性,例如android:focusableInTouchModeandroid:clickableandroid:selectAllOnFocusView属性,例如<selector>

您还可以通过将其背景设置为TextView s(a.k.a。{{1}} drawables)来自定义视图的外观。