Android Button设置边框 和背景

569 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

  1. 效果图 在这里插入图片描述

自定义背景颜色和边框 ,用drawable实现 layer-list 里面包含2个item背景 如果边框颜色不加圆角 ==corners==,则出现直角背景里面是圆角的边框线,当然如果你的主背景颜色是白色,那么就不会出现这种情况。如果是其他颜色不要忘记两个item都给corners值

  1. textview_back_left
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
   //边框颜色处理
    <item>
        <shape>
            <solid android:color="#1B5EEA" />
            //边框圆角
            <corners
                android:bottomLeftRadius="15px"
                android:topLeftRadius="15px"/>
        </shape>
    </item>
    //背景颜色处理
    <item
        android:bottom="1px"
        android:left="1px"
        android:right="-2px"
        android:top="1px">
        <shape>
        //背景圆角
            <corners
                android:bottomLeftRadius="15px"
                android:topLeftRadius="15px"/>
            <stroke
                android:width="1px"
                android:color="#000000" />
        </shape>
    </item>
</layer-list>

  1. textview_back_right
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <solid android:color="#999999" />
            <corners
                android:bottomRightRadius="15px"
                android:topRightRadius="15px"/>
        </shape>
    </item>
    <item
        android:bottom="1px"
        android:left="1px"
        android:right="1px"
        android:top="1px">
        <shape>
            <corners
                android:topRightRadius="15px"
                android:bottomRightRadius="15px"
                />
            <stroke
                android:width="1px"
                android:color="#000000" />
        </shape>
    </item>
</layer-list>
  1. 主布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/gray_white">
    <LinearLayout
        android:id="@+id/friend_ll_top"
        android:layout_marginTop="20dp"
        android:orientation="horizontal"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/tv_friend_add"
            android:padding="10dp"
            android:gravity="center"
            android:textStyle="bold"
            android:background="@drawable/textview_back_right"
            android:textColor="@color/white"
            android:textSize="14sp"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="@string/addfriend"
            />
        <Button
            android:textStyle="bold"
            android:id="@+id/tv_friend_message"
            android:padding="10dp"
            android:gravity="center"
            android:background="@drawable/textview_back_left"
            android:textColor="@color/black"
            android:textSize="14sp"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="信息"
            />
    </LinearLayout>
    <TextView
        android:textColor="@color/gray"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:text="@string/tishi"
        android:layout_below="@+id/friend_ll_top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
</RelativeLayout>

点击事件

代码点击 Drawable

 //点击确定 背景变蓝色
                    Resources resources=getContext().getResources();
                    Drawable drawable=resources.getDrawable(R.drawable.dialog_brack_message2);
                    message.setBackgroundDrawable(drawable);
                    message.setTextColor(Color.parseColor("#ffffff"));//字体白色
                    //点击确定 取消背景变白色
                    Drawable drawable1=resources.getDrawable(R.drawable.dialog_message2);
                    add.setBackgroundDrawable(drawable1);
                    add.setTextColor(Color.parseColor("#000000"));//字体黑色
  • 以上是整个流程