gpt4 book ai didi

android - 按钮形状以及选择器标签

转载 作者:行者123 更新时间:2023-11-29 20:20:28 25 4
gpt4 key购买 nike

我正在尝试使用嵌套形状标签的形状选择器标签,这样我可以使按钮的角变圆,并在聚焦/按下时更改按钮状态。我还在 layout.xml 中使用了带有按钮的 cardView 小部件。但是按钮的边角整形不起作用。下面是 button_states.xml 的代码:-

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@color/grey">
<shape>
<solid android:color="#ffffff" />
<corners
android:topLeftRadius="7dp"
android:topRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:bottomRightRadius="7dp"
/>
</shape>
</item><!-- pressed -->
<item android:state_focused="true"
android:drawable="@color/orange">
<shape>
<solid android:color="#ffffff" />
<corners
android:topLeftRadius="7dp"
android:topRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:bottomRightRadius="7dp"
/>
</shape>
</item> <!-- focused -->
<item android:drawable="@color/green">
<shape>
<solid android:color="#ffffff" />
<corners
android:topLeftRadius="7dp"
android:topRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:bottomRightRadius="7dp"
/>
</shape>
</item> <!-- default -->
</selector>

请帮帮我...

最佳答案

首先,您的 button_bg.xml 将作为:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true"
android:state_pressed="false"
android:drawable="@drawable/rounded" />

<item android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/gradient" />

<item android:state_focused="false"
android:state_pressed="true"
android:drawable="@drawable/gradient" />

<item android:drawable="@drawable/rounded" />

</selector>

然后是 rounded.xml 角。

<shape xmlns:android="http://schemas.android.com/apk/res/android" >    
<solid android:color="#BABABA"/>
<corners android:radius="15px"/>
<padding android:left="1dp" android:top="1dp" android:right="1dp" android:bottom="1dp" />

如果gradient.xml:

 <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#BABABA"/>
<corners android:radius="15px"/>
<padding android:left="1dp" android:top="1dp" android:right="1dp" android:bottom="1dp" />
</shape>
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:angle="90"
android:startColor="#880f0f10"
android:centerColor="#880d0d0f"
android:endColor="#885d5d5e"/>

<corners
android:radius="15px" />
</shape>
</item>
</layer-list>

将 button_bg 指定为 XML 中按钮的背景。它将同时产生圆角和点击效果。

关于android - 按钮形状以及选择器标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33251554/

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