Android中常常使用shape来定义控件的一些显示属性来美化UI;

shape的常用属性有:

(1)solid:填充,设置填充的颜色;

(2)stroke:描边,设置边界的宽度、颜色等;

(3)corners:圆角,五个属性,全部设置的话,会覆盖;

  android:radius="20dp"                          设置四个角的半径

  android:topLeftRadius="20dp"              设置左上角的半径 
  android:topRightRadius="20dp"            设置右上角的半径 
  android:bottomLeftRadius="20dp"        设置右下角的半径 
  android:bottomRightRadius="20dp"      设置左下角的半径

(4)padding:定义内容离边界的距离,其中的属性类似于android:padding_left,android:padding_right;

(5)gradient:对应颜色渐变;当设置填充颜色后,无渐变效果,android:angle 是指从哪个角度开始变,angle的值必须是45的倍数(包括0),仅在type="linear"有效,不然会报错;

(6)size:设置大小;

例如:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/hello_world"
        android:textSize="30sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/roundButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:background="@layout/shape1"
            android:text="@string/button1" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:background="@layout/shape2"
            android:text="@string/button1" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:background="@layout/shape3"
            android:text="@string/button1" />
    </LinearLayout>

</LinearLayout>

shape1.xml:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <!-- 填充的颜色 -->

    <solid android:color="#99FFFF" />

    <!-- 设置按钮的四个角为弧形 -->

    <corners android:radius="20dp" />

    <!-- padding:Button里面的文字与Button边界的间隔 -->

    <padding
        android:bottom="10dp"
        android:left="10dp"
        android:right="10dp"
        android:top="10dp" />

</shape>

shape2.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <!-- 填充的颜色 -->

    <solid android:color="#FFCC66" />

    <!-- 设置按钮的左下角和右下角是圆形边框 -->

    <corners
        android:bottomLeftRadius="20dp"
        android:bottomRightRadius="20dp" />

    <!-- 描边 -->

    <stroke
        android:width="1dp"
        android:color="#000000" />

    <!-- padding:Button里面的文字与Button边界的间隔 -->

    <padding
        android:bottom="10dp"
        android:left="10dp"
        android:right="10dp"
        android:top="10dp" />

</shape>

shape3.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <!-- 填充的颜色 -->

    <solid android:color="#996600" />

    <!-- 设置按钮的左上角和右上角为圆形边框 -->

    <corners
        android:topLeftRadius="20dp"
        android:topRightRadius="20dp"
        />

    <!-- padding:Button里面的文字与Button边界的间隔 -->

    <padding
        android:bottom="10dp"
        android:left="10dp"
        android:right="10dp"
        android:top="10dp" />

</shape>

MainActivity.java

package com.xiaozhang.listview2;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

    Button roundButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        roundButton = (Button) findViewById(R.id.roundButton);

        roundButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "你点击了圆角按钮", Toast.LENGTH_LONG)
                        .show();
            }
        });
    }
}

android 通过shape设置圆形按钮的更多相关文章

  1. 44.Android之Shape设置虚线、圆角和渐变学习

    Shape在Android中设定各种形状,今天记录下,由于比较简单直接贴代码. Shape子属性简单说明一下:  gradient -- 对应颜色渐变. startcolor.endcolor就不多说 ...

  2. 使用shape设置android控件只有部分边框有颜色

    <?xml version="1.0" encoding="UTF-8"?> <layer-list xmlns:android=" ...

  3. Drawable实战解析:Android XML shape 标签使用详解(apk瘦身,减少内存好帮手)

    Android XML shape 标签使用详解   一个android开发者肯定懂得使用 xml 定义一个 Drawable,比如定义一个 rect 或者 circle 作为一个 View 的背景. ...

  4. Android GradientDrawable(shape标签定义) 静态使用和动态使用(圆角,渐变实现)

    Android GradientDrawable使用优势: 1. 快速实现一些基本图形(线,矩形,圆,椭圆,圆环) 2. 快速实现一些圆角,渐变,阴影等效果 3. 代替图片设置为View的背景 4. ...

  5. android给View设置边框 填充颜色 弧度

    <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http: ...

  6. Android中Shape的使用

    先看一下文档对Shape Drawable的描述: Shape Drawable An XML file that defines a geometric shape, including color ...

  7. Android之shape属性详解

    有时候 ,为了满足一些需求,我们要用到 shape 去定义 一些背景,shape 的用法 跟图片一样 ,可以给View设置 Android:background="@drawable/sha ...

  8. 使用shape设置只有部分边框有颜色

    <?xml version="1.0" encoding="UTF-8"?> <layer-list xmlns:android=" ...

  9. Android中shape属性详解

    一.简单使用 刚开始,就先不讲一堆标签的意义及用法,先简单看看shape标签怎么用. 1.新建shape文件 首先在res/drawable文件夹下,新建一个文件,命名为:shape_radius.x ...

随机推荐

  1. iperf网络测试工具

    iperf https://sourceforge.net/projects/iperf/ http://downloads.es.net/pub/iperf/ https://github.com/ ...

  2. final(最终、修饰符)

    /* final(最终.修饰符) final关键字的用法: 1. final关键字修饰一个基本类型的变量时,该变量不能重新赋值,第一次的值为最终的. 2. fianl关键字修饰一个引用类型变量时,该变 ...

  3. PHP magic_quotes_gpc

    大多的PHP程序,都有这样的逻辑: 如果发现php.ini配置为不给GPC变量自动添加转义斜线,则PHP自动为GPC添加转义斜线 但是事实上,这是错误的,因为它改变了GPC变量原来的值. 有这个遗留习 ...

  4. Java里的接口

    Java里面由于不允许多重继承,所以如果要实现多个类的功能,则可以通过实现多个接口来实现. Java接口和Java抽象类代表的就是抽象类型,就是我们需要提出的抽象层的具体表现.OOP面向对象的编程,如 ...

  5. Heritrix源码分析(十四)

    近段时间在搞定Lucene的一些问题,所以Heritrix源码分析暂时告一段落.今天下午在群里有同学提到了Heritrix异常终止的问题以及让Heritrix不停的抓取(就是抓完一遍后载入种子继续抓取 ...

  6. C#自定义List类

    代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespac ...

  7. MySQL的MyISAM和InnoDB对比及优化(转)

    MyISAM和InnoDB是在使用MySQL最常用的两个表类型,各有优缺点,视具体应用而定.基本的差别为:MyISAM类型不支持事务处理等高级处理,而InnoDB类型支持.MyISAM类型的表强调的是 ...

  8. JavaScript对象属性 constructor

     对象属性 constructor 属性返回对创建此对象的数组函数的引用; constructor(构造函数) 在对象创建或实例化时候被调用的方法.通常使用该方法来初始化数据成员和所需资源.构造函数不 ...

  9. 使用angularjs中ng-repeat的$even与$odd属性时的注意事项

    JavaScript中数组的索引是从0开始的,因此我们再取奇偶的时候需要用!$even和!$odd来将$even和$odd的布尔值反转 下面给出一个实例: 使用$odd和$even来制作一个红蓝相间的 ...

  10. 【转】C++中的位拷贝与值拷贝

    [转]http://blog.csdn.net/liam1122/article/details/1966617 为了便于说明我们以String类为例: 首先定义String类,而并不实现其成员函数. ...