Android 自定义圆角按钮

来源:互联网 发布:日军退子弹 知乎 编辑:程序博客网 时间:2024/05/29 16:42

Android 中自定义的控件经常要用到,下面是比较简单基础的。例子

res/drawable 目录下新建名为btn_bg.xml文件

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"        android:shape="rectangle"><!--定义按钮的背景的颜色-->    <solid android:color="#00CCFF"/>    <!--设置按钮的每个角的弧度-->    <corners android:radius="5dp"/>    <!--按钮文字与边界的距离注意的地方,之前没有用到过-->    <padding android:bottom="5dp"             android:left="5dp"             android:right="5dp"             android:top="5dp"/></shape>


main.xml布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:orientation="vertical"              android:layout_width="fill_parent"              android:layout_height="fill_parent"        >    <TextView            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:text="圆角按钮"            android:textSize="25dp"            />    <Button android:layout_width="match_parent"            android:layout_height="80dp"            android:background="@drawable/btn_bg"→背景图片引用            android:text="自定义圆角按钮"            android:layout_marginLeft="20dp"            android:layout_marginRight="20dp"    style="?android:attr/buttonStyleInset"/></LinearLayout>




扩展:
 style="?android:attr/buttonStyleInset"


@android:attr/button:普通按钮的效果
@android:attr/buttonBarButtonStyle:按钮栏的按钮效果
@android:attr/buttonBarStyle:按钮栏效果
@android:attr/buttonStyle:按钮的效果
@android:attr/buttonStyleInset:插入按钮的效果
@android:attr/buttonStyleSmall:小型按钮的效果
注:有些效果需要高级android SDK版本支持

@android:attr/buttonStyleToggle:选项按钮的效果


后续添加补充内容



源码地址:http://download.csdn.net/detail/csdnyuandaimaxuexi/9105529

0 0
原创粉丝点击