2017-01-28 4 views
1

У меня есть кнопка, которая определена, как показано ниже:Добавить иконку в кнопку с пользовательским стилем

<Button 
    android:id="@+id/fab" 
    android:layout_width="60dp" 
    android:layout_height="60dp" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:background="@drawable/my_style" 
    android:src="@drawable/ic_register_fab" 
    android:layout_gravity="end|bottom" 
    android:layout_marginRight="15dp" 
    android:layout_marginBottom="15dp" 
    /> 

my_style.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval" 
> 
<solid android:color="@color/path_primary"/> 
<stroke 
    android:width="2dp" 
    android:color="@color/white" 
    /> 
</shape> 

, когда я хочу, чтобы добавить иконку моей кнопки ввода android:src="..." не работает ни в main_activity.xml, ни в my_style.xml

Как установить значок для моей кнопки? Спасибо.

ответ

0

Кнопка не может иметь изображение. Для этого существует класс ImageButton.

код будет выглядеть именно так, как вы писали, вам просто нужно изменить класс IMAGEBUTTON

<ImageButton 
    android:id="@+id/fab" 
    android:layout_width="60dp" 
    android:layout_height="60dp" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:background="@drawable/my_style" 
    android:src="@drawable/ic_register_fab" 
    android:layout_gravity="end|bottom" 
    android:layout_marginRight="15dp" 
    android:layout_marginBottom="15dp" 
    /> 

Здесь у вас есть documentation и здесь и example

Смежные вопросы