Android基础知识之控件系列(2)——Button及自定义背景

这一次,我倾尽所有,换你一世陪伴。

今天这篇列出常用控件Button的几种样式,只是简单列几个属性的,其他的自己研究吧。废话不多说,直接上效果图加代码。

这里写图片描述

  • activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:orientation="vertical"
        tools:context=".MainActivity">

        <Button
            android:text="默认Button样式"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"/>

        <Button
            android:text="背景设为空"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@null"
            android:layout_marginBottom="10dp"/>

        <Button
            android:text="设置了背景色#CCCCCC"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#CCCCCC"
            android:layout_marginBottom="10dp"/>
        <Button
            android:text="带有alpha值的Butotn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:alpha="0.5"
            android:layout_marginBottom="10dp"/>

        <Button
            android:text="形状是自定义的xml"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/circle_btn_shape"
            android:layout_marginBottom="10dp"/>

        <Button
            android:text="背景是自定义的xml文件"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/button_style"
            android:layout_marginBottom="10dp"/>

    </LinearLayout>
</ScrollView>

其中有两个Button的background是自定义的两个xml下面给出来,这两个xml是放在drawable文件目录下的。

  • button_style.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape>
            <gradient
                android:startColor="#0d76e1"
                android:endColor="#0d76e1"
                android:angle="270" />
            <stroke
                android:width="1dip"
                android:color="#f403c9" />
            <corners
                android:radius="2dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>

    <item android:state_focused="true">
        <shape>
            <gradient
                android:startColor="#ffc2b7"
                android:endColor="#ffc2b7"
                android:angle="270" />
            <stroke
                android:width="1dip"
                android:color="#f403c9" />
            <corners
                android:radius="2dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>

    <item>
        <shape>
            <gradient
                android:startColor="#000000"
                android:endColor="#ffffff"
                android:angle="180" />
            <stroke
                android:width="1dip"
                android:color="#f403c9" />
            <corners
                android:radius="5dip" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>
  • circle_btn_shape
<?xml version="1.0" encoding="utf-8"?>
<shape

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:shape="oval">

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

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

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

    <!-- android:radius 弧形的半径 -->

    <corners android:radius="360dip"/>



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

    <padding

        android:left="10dp"

        android:top="10dp"

        android:right="10dp"

        android:bottom="10dp"

        />

</shape>

部分内容摘自()。

  • 可关注微信公众号(zhudekoudai 、smart_android)
  • QQ群号: 413589216
  • 专注Android分享:http://www.codernote.top/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值