如何为按钮添加边框?在不使用图像的情况下,有可能做到这一点吗?
当前回答
创建可拉的/ 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" />
其他回答
请看这里关于创建一个可绘制的形状 http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape
一旦你这样做了,在XML中为你的按钮集android:background="@drawable/your_button_border"
步骤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"
/>
在Material组件库中,只需使用带有Widget.MaterialComponents.Button.OutlinedButton样式的MaterialButton。
你可以使用strokeColor和strokeWidth属性自定义颜色和宽度
<com.google.android.material.button.MaterialButton
....
style="?attr/materialButtonOutlinedStyle"
app:strokeColor="@color/colorPrimary"/>
使用Jetpack组合使用outline button。 使用border属性自定义宽度和颜色。
OutlinedButton(
onClick = { },
border = BorderStroke(1.dp, Color.Blue),
) {
Text(text = "BORDER")
}
在可绘制文件夹中创建一个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" />
推荐文章
- Android - SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度
- TextView的字体大小在Android应用程序改变字体大小从本机设置
- 如何模拟Android杀死我的进程
- 禁用EditText闪烁光标
- Android Eclipse -无法找到*.apk
- 设置TextView文本从html格式的字符串资源在XML
- 如何允许所有网络连接类型HTTP和HTTPS在Android(9)馅饼?
- Android加载JS包失败
- Android Studio, logcat在应用程序关闭后清理
- 在android中从上下文获取活动
- 无法解析主机"<URL here>"没有与主机名关联的地址
- getActivity()在Fragment函数中返回null
- 按钮背景是透明的
- 在Mac OS X上哪里安装Android SDK ?
- 我如何获得图像缩放功能?