2015-11-21 19 views
5

Tôi đang tạo nút Android, sử dụng XML sau.Màu viền nút Android

Đầu ra trên Xem trước thiết kế, hiển thị như bên dưới. enter image description here

Tuy nhiên khi tôi chạy trên Thiết bị, Samsung Duos. Nó cho thấy hoàn toàn khác nhau. enter image description here

Làm cách nào tôi có thể đặt đường viền.

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

    <stroke 
     android:width="3dp" 
     android:color="#d78d79" /> 

</shape> 

Tôi cũng gặp lỗi trong XML nhưng hoạt động tốt. Không có gì hiển thị khi tôi di chuột lên lỗi.

enter image description here

Có thể giúp gì không?

+0

Hiển thị nội dung '@ drawable/button_border' –

Trả lời

9

Thêm <solid android:color="@android:color/transparent" /> như một phần tử con trong phần tử shape bạn

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle" > 
    <solid android:color="@android:color/transparent" /> 
    <stroke 
     android:width="3dp" 
     android:color="#d78d79" /> 

</shape> 
+0

Tại sao lại có sự khác biệt về thiết kế và thiết bị? –

+0

Đó là 'Samsung' ... tùy biến chủ đề android ... Tôi không biết. Vui lòng kiểm tra xem thiết lập nó có trong suốt sẽ giúp –

+0

Bạn có thể thay đổi XML không? Như Nếu tôi sử dụng @android: màu hai lần, do đó, nhận được lỗi. –

6

Tôi thường làm điều gì đó như thế này!

Bạn có thể thực hiện tất cả điều này trong một XML nhưng tôi sẽ chỉ cho bạn cách để hiểu rõ hơn.

simple_button.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item 
     android:state_pressed="true" 
     android:drawable="@drawable/button_pressed"/> 

    <item 
     android:state_enabled="false" 
     android:drawable="@drawable/button_disabled"/> 
    <item 
     android:state_enabled="true" 
     android:drawable="@drawable/button_enabled"/> 

</selector> 

button_pressed.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <corners android:radius="2dp" /> 
    <solid android:color="#20FF5252" /> 
    <padding 
     android:bottom="0dp" 
     android:left="0dp" 
     android:right="0dp" 
     android:top="0dp" /> 
    <size 
     android:width="100dp" 
     android:height="35dp" /> 
    <stroke 
     android:width="1dp" 
     android:color="#1DE9B6" /> 
</shape> 

button_disabled.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > 
    <corners 
     android:radius="14dp" 
     /> 
    <solid 
     android:color="@android:color/transparent" 
     /> 
    <padding 
     android:left="0dp" 
     android:top="0dp" 
     android:right="0dp" 
     android:bottom="0dp" 
     /> 
    <stroke 
     android:width="8dp" 
     android:color="#1DE9B6" 
     /> 
</shape> 

button_enabled.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > 
    <corners 
     android:radius="2dp" 
    /> 
    <solid 
     android:color="@android:color/transparent" 
    /> 
    <padding 
     android:left="0dp" 
     android:top="0dp" 
     android:right="0dp" 
     android:bottom="0dp" 
    /> 
    <size 
     android:width="260dp" 
     android:height="50dp" 
    /> 

    <stroke 
     android:width="1dp" 
     android:color="#1DE9B6" 
    /> 
</shape> 

styles.xml

<style name="Widget.Button.Simple" parent="android:Widget"> 
     <item name="android:gravity">center_vertical|center_horizontal</item> 
     <item name="android:background">@drawable/simple_button</item> 
     <item name="android:textAppearance">?android:textAppearanceMedium</item> 
     <item name="android:textColor">#1DE9B6</item> 
     <item name="android:textStyle">bold</item> 

    </style> 

Cách sử dụng

<Button 
     android:id="@+id/btn_simple" 
     style="@style/Widget.Button.Simple" 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:layout_margin="20dp" 
     android:text="Button" /> 

Tôi hy vọng nó sẽ giúp!

+1

Cách tiếp cận tốt! Upvote :) –

Các vấn đề liên quan