【发布时间】:2017-08-31 06:57:28
【问题描述】:
我想在按下按钮时更改背景颜色和文本颜色(它应该保持状态,直到再次按下按钮)。此外,如果再次按下,我需要按钮的旧状态。 请大家帮帮我
这是我尝试过的代码
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/beco_white"
android:layout_marginTop="@dimen/dp10"
android:orientation="horizontal">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/button_oberon_mall"
android:layout_width="92dp"
android:layout_height="30dp"
android:layout_marginLeft="@dimen/dp10"
android:minWidth="92dp">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/blue_rounded_corner_new"
android:text="Oberon Mall"
android:fontFamily="sans-serif-regular"
android:textAllCaps="false"
android:textColor="@color/beco_black"
android:textSize="13sp"/>
</FrameLayout>
</FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/button_centre_square_mall"
android:layout_width="134dp"
android:layout_height="30dp"
android:layout_marginLeft="@dimen/dp10"
android:layout_marginRight="@dimen/dp10"
android:minWidth="134dp">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/blue_rounded_corner_new"
android:text="Centre Square Mall"
android:fontFamily="sans-serif-regular"
android:textAllCaps="false"
android:textColor="@color/beco_black"
android:textSize="13sp"/>
</FrameLayout>
</FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/button_lulu_mall"
android:layout_width="81dp"
android:layout_height="30dp">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/blue_rounded_corner_new"
android:text="Lulu Mall"
android:fontFamily="sans-serif-regular"
android:textAllCaps="false"
android:textColor="@color/beco_black"
android:textSize="13sp"/>
</FrameLayout>
</FrameLayout>
</LinearLayout>
blue_round_corner_new.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="false">
<shape android:shape="rectangle">
<!--apply button background transparent, full opacity-->
<solid android:color="#dedfe0"/>
<corners android:radius="15dp"/>
</shape>
</item >
<item
android:state_pressed="true">
<shape android:shape="rectangle">
<!--apply button background transparent, full opacity-->
<solid android:color="#4990d0"/>
<corners android:radius="15dp"/>
</shape>
</item>
在此代码中,按下时背景颜色只会改变一段时间(不保持状态)
任何人都可以帮助我。对不起我的英语
【问题讨论】:
-
如果您已将背景设置为该颜色,则必须通过代码设置背景颜色。选择器无法做到这一点
-
“一会儿”是什么意思?该按钮不能神奇地回到旧状态。它要么在重新渲染或发生其他事情时这样做。
-
最好使用外部库来解决这个问题检查这个链接android-arsenal.com/details/1/2143 有帮助吗请告诉我