24

我创建了一个按钮并设置了背景颜色和文本颜色,如下所示。我的问题是:如何设置按钮边框颜色?我想将边框颜色设置为白色

这是我的按钮res -> layout -> main_nav.xml

<Button 
        android:id="@+id/btn_emergency"
        style="@style/buttonStyle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Contact and Emergency" 
        android:onClick="onClickHandleStates" />

这是它的相关样式res -> values -> styles。前 2 个“项目”可以自行正常工作。最后一个“项目”是我尝试将按钮边框更改为白色但没有成功。

<!-- The button styles -->
<style name="buttonStyle">
    <item name="android:textColor">#ffffff</item>
    <item name="android:background">#80000000</item>

    <item name="android:button">
        <shape
            android:shape="rectangle" >
            <stroke
                android:width="0.5dp"
                android:color="#ffffff" />  
        </shape>
    </item>
</style>
4

6 回答 6

40

使用<stroke>元素。在 res/drawable 文件夹中添加这个 xml 文件作为 button_border.xml:

 <?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="#ffffff" />
 </shape>

然后通过调用它

<Button
   android:id="@+id/button1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_margin="10dp"
   android:background="@drawable/button_border"
   android:text="Button" 
/>
于 2013-09-25T16:44:26.950 回答
9

试试这个

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<corners android:radius="1dp" />

<stroke
    android:width="2px"
    android:color="#ffffff" />

</shape>
于 2015-03-16T07:10:50.843 回答
6

您可以使用此在线按钮生成器来自定义您的按钮http://angrytools.com/android/button/

于 2014-05-11T11:15:53.820 回答
5

在此处输入图像描述

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<gradient
    android:angle="270"
    android:endColor="#E8f2fe"
    android:startColor="#E8f2fe" />

<corners android:radius="3dp" />

<stroke
    android:width="2px"
    android:color="#486e9d" />
于 2015-09-10T06:22:32.297 回答
5

尝试材质按钮

根据需要设置 app:strokeWidth, app:strokeColor

<com.google.android.material.button.MaterialButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="@style/Widget.MaterialComponents.Button.OutlinedButton"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:strokeWidth="0.6dp"
    app:cornerRadius="2dp"
    android:text="Reassign"
    android:textColor="@color/colorAccent"
    app:strokeColor="@color/colorAccent"

    />

注意:您需要为材质小部件添加依赖项

//Material Components for Android
implementation 'com.google.android.material:material:1.0.0'
于 2019-07-30T06:59:55.727 回答
0

如果您想要按钮的透明背景,请使用 Shivang Trivedi 的答案,但删除“渐变”部分,如下所示:

<?xml version="1.0" encoding="utf-8"?>   <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="3dp" />
    <stroke android:width="5px" android:color="#ffffff" />  </shape>
于 2019-04-24T09:25:48.697 回答