gpt4 book ai didi

Android - 在 TextView 上设置按下状态和边框 - 只有一个背景

转载 作者:行者123 更新时间:2023-12-01 19:13:48 27 4
gpt4 key购买 nike

我有 3 个 TextView 垂直堆叠在 LinearLayout 中

<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 图像中来解决这个问题。我希望我不需要这样做

谢谢

最佳答案

在选择器中使用呈现边框的 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"

关于Android - 在 TextView 上设置按下状态和边框 - 只有一个背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26891459/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com