Android - 如何为TextView设置按压状态和边框,同时保留原有背景?

3

我有一个垂直排列的LinearLayout,其中包含3个TextViews

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:textSize="@dimen/text_size_large"
        android:text="Item1"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:textSize="@dimen/text_size_large"
        android:text="Item2"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:textSize="@dimen/text_size_large"
        android:text="Item3"/>
</LinearLayout>

我可以将可绘制对象应用于背景属性以呈现边框。
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:top="-2dp" android:right="-2dp" android:left="-2dp">
        <shape>
            <solid android:color="@android:color/transparent" />
            <stroke android:width="1dp" android:color="@color/pale_grey" />
            <padding android:left="24dp" android:right="24dp" 
                       android:top="12dp" android:bottom="12dp" />
        </shape>
    </item>
</layer-list>

我还可以通过可绘制的选择器来应用选择器到背景属性,以便TextView根据按下等状态更改颜色。但是,只有一个背景属性。
我不认为我可以在同一个可绘制对象中应用 XML和 XML。因此,我需要将单个布局元素包装在每个TextView周围,以便可以应用另一个背景吗?
看起来谷歌通过将边框、填充和背景颜色合并到一个可绘制的patch-9图像中来解决了这个问题。我希望我不需要那样做。
谢谢
2个回答

6
在选择器中使用你的XML来设置按下和未按下状态,这些XML将渲染边框。
示例:
<selector     xlmns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/border_unpressed" android:state_pressed="false"/> 
 <item android:drawable="@drawable/border_pressed" android:state_pressed="true"/>
 <item android:drawable="@drawable/border_unpressed" />

</selector>

注意: 请确保将TextView属性android:clickable="true"设置为真。

你是说我应该将背景颜色设置和边框形状和描边设置合并到一个drawable中吗?现在听起来很明显。关于“clickable”,我只是剪裁了大部分的XML以缩小问题规模。 - Pat Long - Munkii Yebee
1
是的,但您应该创建两个xml可绘制对象,一个带有按下效果,另一个带有未按下效果。如果这对您有用,请接受我的答案。 - Sherif El Nady
我被拉到其他事情上了,但是一旦我回来了,我会尝试并肯定会回来接受。 - Pat Long - Munkii Yebee
1
谢里夫。已完成工作,只是希望 Android 不强制我创建多个文件。谢谢。 - Pat Long - Munkii Yebee

1
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:shape="rectangle" >
            <corners
                android:radius="4dp">
            </corners>
            <stroke android:width="1dp" android:color="#ffffff"/>
            <gradient
                android:startColor="#187cb6"
                android:endColor="#187cb6"
                android:angle="270"/>
        </shape>
    </item>
    <item>
        <selector xmlns:android="http://schemas.android.com/apk/res/android">
            <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/gradient" />
            <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/gradient" />
        </selector>
    </item>
</layer-list>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接