温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android button, xml文件定义形状,代码中修改背景颜色

发布时间:2020-06-09 15:16:27 来源:网络 阅读:1322 作者:wehappy168 栏目:移动开发

1. 首先在drawable文件夹定义一个shape.xml文件,内容如下:

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

    <corners
        android:topLeftRadius="10dp"
        android:topRightRadius="10dp"
        android:bottomRightRadius="10dp"
        android:bottomLeftRadius="10dp" />

    <padding
        android:left="30dp"
        android:top="0dp"
        android:right="30dp"
        android:bottom="0dp"
        />
</shape>


2. 在main.xml文件中,button使用这个shape.xml,如下:

<Button
        android:id="@+id/button_next"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="@string/button_next"
        android:textSize="@dimen/label_text_size"
        android:layout_alignParentBottom="true"
        android:layout_centerVertical="true"
        android:gravity="center_horizontal|center_vertical"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:background="@drawable/shape"
        />

这时,该button就显示如shape.xml定义的形状。


3. 在代码中动态修改button 背景颜色,代码如下:

GradientDrawable bgShape = (GradientDrawable)buttonNext.getBackground();
bgShape.setColor(Color.BLUE);


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI