【问题标题】:Change the color of TextView on different states在不同状态下更改 TextView 的颜色
【发布时间】:2017-08-10 05:41:19
【问题描述】:

我想在不同状态下更改 TextView 的颜色,例如按下、聚焦等。 我正在使用以下代码,我需要添加一些更改 TextView 的颜色。

<?xml version="1.0" encoding="UTF-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <stroke android:width="1dp" android:color="#FFFFFF" />
            <corners android:radius="2dp"/>
            <padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" />
        </shape>
    </item>
    <item>
        <ripple
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:color="?android:attr/colorControlHighlight" android:state_pressed="true">
            <item
                android:id="@android:id/mask"
                android:drawable="@color/colorBackgroundDark"></item>
        </ripple>
    </item>
</layer-list>

【问题讨论】:

    标签: android textview android-studio-2.3


    【解决方案1】:

    您是否尝试过使用可绘制选择器?

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/background_category_filter_selected" android:state_enabled="true" android:state_pressed="true"></item>
        <item android:drawable="@drawable/background_category_filter_not_selected" android:state_enabled="true" android:state_pressed="false"></item>
    </selector>
    

    在这个例子中,当按下或不按下按钮时,我有不同的可绘制对象。您还可以使用其他状态,例如活跃、专注…… 后来我就这样做了

             <Button
                    android:id="@+id/first_day_forward"
                    android:layout_width="0dp"
                    android:layout_height="36dp"
                    android:layout_marginEnd="15dp"
                    android:layout_marginTop="2dp"
                    android:layout_weight="2"
                    android:background="@drawable/background_filter_button_square"
                    android:text="&gt;"
                    android:textAppearance="@style/fontMontseratReg"
                    android:textColor="@drawable/filter_text_button" />
    

    在示例中我使用了一个按钮,但我认为它也适用于文本视图。

    要更改 xml shape 标记中的填充,请使用 solid,如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <solid android:color="#ffffff"/>
        <corners android:radius="3dp"/>
    </shape>
    

    在你的情况下,你可以这样做:

    <selector
        xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="false">
            <shape>
                <stroke android:width="1dp" android:color="#FFFFFF" />
                <corners android:radius="2dp"/>
                <padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" />
            </shape>
        </item>
        <item android:state_pressed="true">
            <ripple
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:color="?android:attr/colorControlHighlight" >
                <item
                    android:id="@android:id/mask"
                    android:drawable="@color/colorBackgroundDark"></item>
            </ripple>
        </item>
    </selector>
    

    【讨论】:

    • 感谢您的回复。我试过选择器,但它没有给我任何输出。
    • 我已经展示了我的代码,你能告诉我我要把选择器放在哪里吗??
    • 别忘了标记为答案,这样它也可以帮助其他人:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-19
    • 2015-02-14
    • 2015-04-08
    • 1970-01-01
    • 2020-03-26
    • 1970-01-01
    相关资源
    最近更新 更多