android - Android 按钮上的边框颜色

标签 android xml eclipse button border

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

这是我的 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>

最佳答案

使用 <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" 
/>

关于android - Android 按钮上的边框颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19010573/

相关文章:

java - 关于任务 onPostExecute

java - 如何知道log4j的输出来自哪里?

C++ Eclipse 调试器 : "Cannot find bounds of current function" and doesn't stop at breakpoints

android - 错误致命信号 11

java - Android查看动态大小

android - android(androidx) 中的 RecyclerView 依赖项?

android - 针对不同 DPI 的 OpenGL 表面 View 缩放

c# - XMLWriterTraceListener 生成文本墙而不是格式化的 XML

javascript - 在 IE9 而不是 Chrome 中使用 jQuery 解析 XML 时缺少 innerHTML

Android:使用 InputStream 打开位于 SD 中的文件时出现问题