android 控件加圆角

时间:2024-12-24 08:35:44

1.新建一个radius_border.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF" /><!--solid 背景填充颜色 -->
<stroke android:width="1dip" android:color="#aea594" /><!-- 描边,边框宽度、颜色 -->
<corners android:topLeftRadius="10dp"
android:topRightRadius="10dp"
android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"
/><!-- 边角圆弧的半径 -->
<padding android:left="3dp" android:top="3dp" android:right="3dp" android:bottom="3dp" /><!-- 四周留出来的空白 --> </shape>

在项目中我们可能边框只需要加一边的办法

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:left="-2dp"
android:right="-2dp"
android:top="-2dp">
<shape>
<solid android:color="#ffffff"/>
<stroke
android:width="1dp"
android:color="#ff0000"/>
</shape>
</item>
</layer-list>

2.在代码中添加一句:

TextView.setBackgroundResource(R.drawable.radius_border);

这里可能回报一句错误element shape doesn't required attribute android:layout_height

解决方法:

radius_border.xml移动到drawable中即可

3.button根据焦点改变北京图片

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" > <!--android:drawable="@drawable/ic_launcher"-->
<item android:state_pressed="true" > <shape> <solid android:color="#ff1234" ></solid>
<corners android:topLeftRadius="5dp"
android:topRightRadius="5dp"
android:bottomRightRadius="5dp"
android:bottomLeftRadius="5dp"
/> </shape> </item> <!--android:state_focused是获得焦点android:state_pressed是点击--> <item android:state_focused="true"
android:drawable="@drawable/ic_launcher" > <shape> <solid android:color="#ffaeff5d"></solid>
<corners android:topLeftRadius="5dp"
android:topRightRadius="5dp"
android:bottomRightRadius="5dp"
android:bottomLeftRadius="5dp"
/> </shape> </item>
<item>
<shape> <solid android:color="@color/colorNav"></solid>
<corners android:topLeftRadius="5dp"
android:topRightRadius="5dp"
android:bottomRightRadius="5dp"
android:bottomLeftRadius="5dp"
/> </shape>
</item> </selector>