Android fully shaped Button

前端 未结 1 803
长发绾君心
长发绾君心 2021-01-20 23:45

I tried to change the shape of some Android components like Button or View. What I can do is assign a shape to the background with the onDraw-Method. Well, it looks like reshape

相关标签:
1条回答
  • 2021-01-21 00:25

    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>
    
    0 讨论(0)
提交回复
热议问题