Android-在TextView上设置按下状态和边框-仅一个背景 [英] Android - Setting Pressed State and Border on a TextView - Only one Background

查看:149
本文介绍了Android-在TextView上设置按下状态和边框-仅一个背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have 3 TextViews stacked vertically in a 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>

我可以将drawable应用于background属性以渲染边框

I could apply a drawable to the background attribute to render a Border

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

我还可以通过可绘制对象将选择器应用于background属性,以便TextView根据诸如Pressed之类的状态更改颜色.但是,只有一个背景属性.

I could also apply a selector, via a drawable, to the background property so that the TextView changed color depending on states such as Pressed. However there is only one background attribute.

我不相信我可以在同一个可绘制对象中应用< shape> xml和< selector> xml.因此,我是否需要在每个TextView周围包裹一个布局元素,以便可以应用其他背景?

I do not believe i can apply the <shape> xml and the <selector> xml within the same drawable. Therefore do I need to wrap a single layout element around each TextView just so can apply another background?

看起来Google通过将边框,填充和背景色包含到一个可绘制的patch-9图像中来解决了这一问题.我希望我不需要这样做

It looks like google solves this by encompassing border, padding and background colour into one drawable patch-9 image. I was hoping I didn't need to do that

谢谢

推荐答案

在选择器中,设置呈现边框的XML的按下状态和未按下状态

In the selector set the state pressed and unpressed with your XML's that render the border

示例:

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

Note: Make sure to set the TextView attribute android:clickable="true"

这篇关于Android-在TextView上设置按下状态和边框-仅一个背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆