Android Button设置边框 和背景

  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"));//字体黑色
  • 以上是整个流程
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
### 回答1: 要去掉 Android Button边框,可以在布局文件中添如下代码: ```xml <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/transparent" android:text="Button Text" /> ``` 其中,`android:background="@android:color/transparent"` 用于设置 Button背景色为透明,这样就可以去掉边框了。 ### 回答2: Android Button默认有边框,但我们可以通过修改Button的样式来去掉边框。具体步骤如下: 1. 在res文件夹下的drawable目录中创建一个XML文件,例如button_bg.xml。 2. 在button_bg.xml文件中添以下代码: ``` <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_enabled="true"> <shape android:shape="rectangle"> <solid android:color="#ffffff" /> <!-- 设置按钮背景色 --> <corners android:radius="8dp" /> <!-- 设置按钮圆角半径 --> </shape> </item> <item android:state_enabled="false"> <shape android:shape="rectangle"> <solid android:color="#999999" /> <!-- 设置按钮不可用时的背景色 --> <corners android:radius="8dp" /> <!-- 设置按钮圆角半径 --> </shape> </item> </selector> ``` 这里使用了一个selector来为Button设置不同的背景样式,分别对应按钮可用和不可用两种状态。 3. 在layout文件中将Button的background属性设置为刚才创建的XML文件,例如: ``` <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:background="@drawable/button_bg" /> ``` 这样就可以将Button背景样式设置为没有边框的样式了。 以上就是通过修改Button的样式来去掉边框的方法。如果需要更多自定义样式,可以进一步修改button_bg.xml中的代码。 ### 回答3: 要去掉 Android 按钮的边框,可以通过修改按钮的背景属性来实现。首先,我们可以在按钮的布局文件中添如下代码: ```xml <Button android:id="@+id/myButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/transparent" android:text="点击按钮" /> ``` 在上述代码中,我们为按钮设置了一个透明的背景,这将隐藏按钮的边框。此外,你还可以添其他属性来自定义按钮的样式,如 `android:textColor` 修改文本颜色,`android:textSize` 修改文本大小等等。 另外,你也可以使用代码动态地修改按钮的背景属性。在代码中找到按钮并设置背景为透明即可,如下所示: ```java Button myButton = findViewById(R.id.myButton); myButton.setBackground(getDrawable(android.R.color.transparent)); ``` 这样,按钮的边框将被移除。通过上述方法,你可以根据需要自定义 Android 按钮的样式,以满足你的设计需求。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AaVictory.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值