在android中创建带圆角的Button

cod*_*lus 11 android android-layout android-button

在此输入图像描述

我正在尝试创建一个看起来如上图所示的按钮.最初我的想法是创建一个9补丁并将其设置为按钮背景.但是,因为这是一个简单的按钮,我想我们可以在不使用任何图像的情况下以某种方式绘制它.

按钮背景颜色为#0c0c0c边框颜色为#1a1a1a文本颜色为#cccccc

我在SO上发现了一个类似的问题,但它创建了一个渐变 -
Android - 按钮的边框

Sam*_*Sam 23

Android开发人员指南有一个详细的指南:Shape Drawbables.

您也可以gradient从您提供的链接中删除该元素:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="3dp" />
    <stroke android:width="5px" android:color="#1a1a1a" />
</shape>
Run Code Online (Sandbox Code Playgroud)


Rag*_*dan 16

   <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView1"
    android:layout_alignLeft="@+id/textView1"
    android:layout_marginBottom="56dp"
    android:text="Button" 
    android:textColor="#FF0F13"
    android:background="@drawable/bbkg"/>//create bbkg.xml in drawable folder
Run Code Online (Sandbox Code Playgroud)

bbkg.xml //按钮背景

   <?xml version="1.0" encoding="utf-8"?>
  <selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_pressed="true" 
    android:drawable="@drawable/pressed" />
  <item  android:state_focused="false" 
    android:drawable="@drawable/normal" />
  </selector>
Run Code Online (Sandbox Code Playgroud)

normal.xml //按钮背景正常状态

  <?xml version="1.0" encoding="UTF-8"?> 
  <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"> 
  <solid android:color="#10EB0A"/>    
  <stroke android:width="3dp"
        android:color="#0FECFF" /> 
  <padding android:left="5dp"
         android:top="5dp"
         android:right="5dp"
         android:bottom="5dp"/> 
  <corners android:bottomRightRadius="7dp"
         android:bottomLeftRadius="7dp" 
         android:topLeftRadius="7dp"
         android:topRightRadius="7dp"/> 
  </shape>   
Run Code Online (Sandbox Code Playgroud)

pressed.xml //按钮后台按下状态

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="rectangle"> 
<solid android:color="#FF1A47"/>    
<stroke android:width="3dp"
        android:color="#0FECFF"/>

<corners android:bottomRightRadius="7dp"
         android:bottomLeftRadius="7dp" 
         android:topLeftRadius="7dp"
         android:topRightRadius="7dp"/> 
</shape>  
Run Code Online (Sandbox Code Playgroud)