如何为按钮添加边框?在不使用图像的情况下,有可能做到这一点吗?
当前回答
<com.google.android.material.button.MaterialButton
android:id="@+id/addBtn"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="150dp"
android:layout_height="80dp"
android:gravity="center"
android:backgroundTint="@android:color/transparent"
android:textColor="@color/blue"
app:cornerRadius="8dp"
app:strokeColor="@color/blue"
app:strokeWidth="2dp"/>
其他回答
步骤1:创建名为my_button_bg.xml的文件
步骤2:将该文件放在res/drawables.xml中
步骤3:插入以下代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#FFFFFF"
android:endColor="#00FF00"
android:angle="270" />
<corners android:radius="3dp" />
<stroke android:width="5px" android:color="#000000" />
</shape>
步骤4:在需要的地方使用代码“android:background=”@drawable/my_button_bg”,如下所示:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Text"
android:background="@drawable/my_button_bg"
/>
在可绘制文件夹中创建一个button_border.xml文件。
res - drawable button_border xml。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#FFDA8200" />
<stroke
android:width="3dp"
android:color="#FFFF4917" />
</shape>
并添加按钮到您的XML活动布局和设置背景android:background="@drawable/button_border"。
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_border"
android:text="Button Border" />
创建可拉的/ button_green.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#003000"
android:centerColor="#006000"
android:endColor="#003000"
android:angle="270" />
<corners android:radius="5dp" />
<stroke android:width="2px" android:color="#007000" />
</shape>
并指出它是@drawable/button_green:
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/button_green"
android:text="Button" />
我知道这晚了一年,但你也可以创建一个9路径图像 android SDK中有一个工具可以帮助创建这样的图像 请看这个链接:http://developer.android.com/tools/help/draw9patch.html
PS:图像也可以无限缩放
请看这里关于创建一个可绘制的形状 http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape
一旦你这样做了,在XML中为你的按钮集android:background="@drawable/your_button_border"
推荐文章
- 改变开关的“开”色
- 以编程方式将EditText的输入类型从PASSWORD更改为NORMAL,反之亦然
- 如何在隐藏和查看密码之间切换
- 在Android上调整一个大的位图文件到缩放输出文件
- 如何更改Android版本和代码版本号?
- Android Studio突然无法解析符号
- 应用程序重新启动而不是恢复
- 如何设置整个应用程序在纵向模式?
- Android中文本的阴影效果?
- 以编程方式设置TextView的布局权重
- Android -如何覆盖“后退”按钮,所以它不完成()我的活动?
- 如何从通知点击发送参数到一个活动?
- 导航目标xxx对于这个NavController是未知的
- 使用ConstraintLayout均匀间距的视图
- 文件google-services错误。模块根文件夹中缺少Json。谷歌服务插件没有它就不能正常工作。