Android fully shaped Button

丶灬走出姿态 提交于 2019-12-01 22:01:52

You can define the shapes in xml (as you can see here). e.g. :

drawable/sample_shape.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#FFFF0000"
        android:endColor="#80FF00FF"
        android:angle="45"/>
    <padding android:left="7dp"
        android:top="7dp"
        android:right="7dp"
        android:bottom="7dp" />
    <corners android:radius="8dp" />
</shape>

Then you can use the xml description as a drawable for an ImageButton.

drawable/samplebutton.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/sample_shape" /> <!-- pressed -->
    <item android:state_focused="true"
       android:drawable="@drawable/sample_shape" /> <!-- focused -->
    <item android:drawable="@drawable/sample_shape" /> <!-- default -->
</selector>

in your layoutfile:

<ImageButton 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:background="@drawable/samplebutton">
</ImageButton>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!