gpt4 book ai didi

android - 形状和选择器不起作用

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

在我的 android 应用程序中,希望我的按钮为绿色和圆 Angular 矩形,当有人按下按钮时,它应该将颜色更改为灰色。所以我创建了一个带有选择器的 XML 文件,并将其赋予圆 Angular 矩形的形状,但问题是当我启动我的应用程序时,默认情况下颜色不是绿色,而是透明的。此外,当我按下按钮时,它显示矩形形状,所以我认为 shapre 也不起作用。这是我的原色:

<color name="colorPrimary">#669900</color> 

我的自定义按钮.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>

最佳答案

要使矩形变圆,请尝试类似以下代码的操作:

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

关于android - 形状和选择器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39966714/

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