在我的Android应用程序中,希望我的按钮为绿色和圆角矩形,当有人按下按钮时,它应该将颜色更改为灰色。所以我创建了一个带选择器的XML文件并将其赋予圆角矩形的形状,但问题是当我启动应用程序时,默认情况下颜色不是绿色透明的。此外,当我按下按钮时,它显示矩形形状,所以我认为shapre也不起作用。这是我的主要颜色:
<color name="colorPrimary">#669900</color>
我的custom_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<item android:state_focused="true" android:state_pressed="false" android:color="@color/colorPrimary" android:drawable="@color/colorPrimary">
<shape
android:shape="rectangle" android:padding="10dp">
<solid android:color="@color/colorPrimary"/>
<corners
android:bottomRightRadius="15dp"
android:bottomLeftRadius="15dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp"/>
</shape>
</item>
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/gradient" >
<shape
android:shape="rectangle" android:padding="10dp">
<corners
android:bottomRightRadius="15dp"
android:bottomLeftRadius="15dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp"/>
</shape>
</item>
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/gradient" >
<shape
android:shape="rectangle" android:padding="10dp">
<corners
android:bottomRightRadius="15dp"
android:bottomLeftRadius="15dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp"/>
</shape>
</item>
</selector>
我还为渐变做了一个XML文件,它是gradient.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:angle="90"
android:startColor="#880f0f10"
android:centerColor="#880d0d0f"
android:endColor="#885d5d5e"/>
</shape>
</item>
</layer-list>
答案 0 :(得分:0)
我之前做过类似的事情就是我这样做的方式
button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_default" android:state_pressed="false" android:state_focused="false"/>
<item android:drawable="@drawable/button_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/button_pressed" android:state_focused="true" />
</selector>
button_default.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="@dimen/radius" />
<solid
android:color="?attr/colorAccent" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp"
/>
</shape>
button_pressed.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="@dimen/radius" />
<solid
android:color="?attr/colorAccent" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp"
/>
</shape>
值/ styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:buttonStyle">@style/BlueButton</item>
</style>
<style name="BlueButton" parent="android:style/Widget.Button">
<item name="android:background">@drawable/button</item>
<item name="android:textColor">@drawable/button_text_color</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">18sp</item>
</style>
</resources>
我也猜测你可能需要更改文本颜色,所以这里是文件 button_text_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorPrimaryDark" android:state_pressed="false" android:state_focused="false"/>
<item android:color="?attr/colorAccent" android:state_pressed="true"/>
<item android:color="?attr/colorAccent" android:state_focused="true" />
</selector>
答案 1 :(得分:0)
要使矩形变圆,请尝试以下代码:
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="rectangle">
<corners
android:radius="5dp"/>
<stroke
android:width="2dp"
android:color="@color/border_of_rectangle"/>
<solid
android:color="@color/color_inside_rectangle"/>
</shape>
</item>
</layer-list>