如何为按钮添加边框?在不使用图像的情况下,有可能做到这一点吗?


当前回答

•Android官方解决方案

自从Android设计支持v28推出以来,使用MaterialButton很容易创建一个有边框的按钮。该类为构造函数中的按钮提供了更新的Material样式。使用app:strokeColor和app:strokeWidth你可以创建一个自定义边框,如下所示:


1. 当你使用androidx:

build.gradle

dependencies {
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.material:material:1.0.0'
}

•边框按钮:

<com.google.android.material.button.MaterialButton
    style="@style/Widget.AppCompat.Button.Colored"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="MATERIAL BUTTON"
    android:textSize="15sp"
    app:strokeColor="@color/green"
    app:strokeWidth="2dp" />

•未填充边框按钮:

<com.google.android.material.button.MaterialButton
    style="@style/Widget.AppCompat.Button.Borderless"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="UNFILLED MATERIAL BUTTON"
    android:textColor="@color/green"
    android:textSize="15sp"
    app:backgroundTint="@android:color/transparent"
    app:cornerRadius="8dp"
    app:rippleColor="#33AAAAAA"
    app:strokeColor="@color/green"
    app:strokeWidth="2dp" />

2. 当你使用appcompat:

build.gradle

dependencies {
    implementation 'com.android.support:design:28.0.0'
}

style.xml

确保您的应用程序主题继承自theme。而不是Theme.AppCompat。

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
    <!-- Customize your theme here. -->
</style>

•边框按钮:

<android.support.design.button.MaterialButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="MATERIAL BUTTON"
    android:textSize="15sp"
    app:strokeColor="@color/green"
    app:strokeWidth="2dp" />

•未填充边框按钮:

<android.support.design.button.MaterialButton
    style="@style/Widget.AppCompat.Button.Borderless"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="UNFILLED MATERIAL BUTTON"
    android:textColor="@color/green"
    android:textSize="15sp"
    app:backgroundTint="@android:color/transparent"
    app:cornerRadius="8dp"
    app:rippleColor="#33AAAAAA"
    app:strokeColor="@color/green"
    app:strokeWidth="2dp" />


视觉效果

其他回答

我知道这晚了一年,但你也可以创建一个9路径图像 android SDK中有一个工具可以帮助创建这样的图像 请看这个链接:http://developer.android.com/tools/help/draw9patch.html

PS:图像也可以无限缩放

在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")
}

步骤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"
    />

如果你的按钮不需要透明的背景,那么你可以使用框架布局创建一个边框的错觉。只需调整FrameLayout的“padding”属性来改变边框的厚度。

<FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="1sp"
        android:background="#000000">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Your text goes here"
            android:background="@color/white"
            android:textColor="@color/black"
            android:padding="10sp"
            />
</FrameLayout>

我不确定形状xml文件是否具有动态可编辑的边界颜色。但是我知道有了这个解决方案,你可以通过设置FrameLayout背景动态地改变边框的颜色。

在可绘制文件夹中创建一个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" />