2012-10-07 59 views
11

enter image description hereTạo nút có góc tròn trong android

Tôi đang cố gắng tạo nút trông giống như hình trên. Ban đầu, ý tưởng của tôi là tạo ra một bản vá 9 và đặt nó làm nền nút. Nhưng vì, đây là một nút đơn giản, tôi nghĩ chúng ta bằng cách nào đó có thể vẽ nó mà không sử dụng bất kỳ hình ảnh nào.

Màu nền nút là # 0c0c0c Màu sắc đường viền là # 1a1a1a Màu văn bản là #CCCCCC

Tôi tìm thấy một câu hỏi tương tự trên SO nhưng điều đó sẽ tạo ra một gradient -
Android - border for button

+1

"Màu nền nút là # 0c0c0c Màu đường viền là # 1a1a1a" - có thể * một * bộ màu, nhưng bạn cần nhiều hơn thế. Nếu chúng ta giả định rằng bạn muốn những màu đó là trạng thái bình thường, màu của bạn cho trạng thái ép là gì? Trạng thái bị vô hiệu hóa? Trạng thái tập trung? Có thể có các trạng thái khác mà nền 'Nút' mặc định xử lý vượt ra ngoài những điều đó, nhưng tôi sẽ bắt đầu với chúng. – CommonsWare

Trả lời

23

Các Hướng dẫn dành cho nhà phát triển Android có hướng dẫn chi tiết về điều này: Shape Drawbables.

Bạn cũng có thể chỉ cần loại bỏ các yếu tố gradient từ liên kết mà bạn cung cấp:

<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> 
+1

cảm ơn :) đã thêm điều này để có được bg rắn coderplus

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 

bbkg.xml // nút nền

<?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> 

normal.xml // nền nút bình thường tiểu bang

<?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> 

pressed.xml // nền nút bấm bang

<?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> 
+1

Bạn có thể thay đổi đường viền của nút cũng như nút backgroud ở trạng thái bình thường và được nhấn. Chỉ cần sử dụng mã màu phù hợp – Raghunandan

3

Nếu bạn muốn một cái gì đó như thế này

Nút xem trước

đây là mã.

1.Create một file xml trong thư mục drawable của bạn như mybutton.xml và dán đánh dấu sau:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
<item android:state_pressed="true" > 
    <shape android:shape="rectangle" > 
     <corners android:radius="3dip" /> 
     <stroke android:width="1dip" android:color="#5e7974" /> 
     <gradient android:angle="-90" android:startColor="#345953" android:endColor="#689a92" />    
    </shape> 
</item> 
<item android:state_focused="true"> 
    <shape android:shape="rectangle" > 
     <corners android:radius="3dip" /> 
     <stroke android:width="1dip" android:color="#5e7974" /> 
     <solid android:color="#58857e"/>  
    </shape> 
</item> 
<item > 
    <shape android:shape="rectangle" > 
     <corners android:radius="3dip" /> 
     <stroke android:width="1dip" android:color="#5e7974" /> 
     <gradient android:angle="-90" android:startColor="#8dbab3" android:endColor="#58857e" />    
    </shape> 
</item> 
</selector> 

2.Now sử dụng drawable này cho nền tảng của quan điểm của bạn. Nếu nút xem là một cái gì đó như thế này:

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="10dp" 
    android:textColor="#ffffff" 
    android:background="@drawable/mybutton" 
    android:text="Buttons" />