开发者

Button with background image and corner radious in android

I am trying to make a button with a backl ground image. Again I want to apply some state style with selector xml. I have used this xml below as style and the other one as selector. But the background image is not showing up. Also I want to apply some corner radious to the button

Please help.

<style name="HomeMenuButton" parent="@android:style/Widget.Button">
    <item name="android:gravity">center_vertical|center_horizontal</item>
    <item name="android:textColor">#FFFFFFFF</item>
    <item name="android:shadowColor">#FF000000</item>
    <item name="android:shadowDx">0</item>
    <item name="android:shadowDy">-1</item>
    <item name="android:shadowRadius">5</item>
    <item name="android:textSize">16dip</item>
    <item name="android:textStyle">bold</item>
    <item name="android:background">@drawable/button_back</item>
    <item name="android:focusable">true</item>
    <item name="android:clickable">true</item>
</style>


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item >
        <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
            <stroke android:width="2dp" android:color="@color/red" />
            <solid android:color="#FFFFFFFF"/>
            <padding android:left="5dp" android:top="2dp" 
                android:right="5dp" android:bottom="2dp" /> 
            <corners android:radius="15dp" /> 

        </shape>
    </item>
    <item android:state_pressed="true" >         
  开发者_高级运维      <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
            <stroke android:width="2dp" android:color="@color/red" />
            <solid android:color="@color/blue"/>
            <padding android:left="5dp" android:top="2dp" 
                android:right="5dp" android:bottom="2dp" /> 
            <corners android:radius="15dp" /> 

        </shape>    
    </item> 
</selector>


I have just tried your code and it shows the background as you intended.

Have you added a android:style attribute into you layout xml? Something like this:

<Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello world!"
    style="@style/HomeMenuButton"
/>

Moreover your original state drawable doesn't show pressed state. You may need to change it a little bit by moving the pressed state item to before the normal state.

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

    <item android:state_pressed="true" >         
        <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
            <stroke android:width="2dp" android:color="@color/red" />
            <solid android:color="@color/blue"/>
            <padding android:left="5dp" android:top="2dp" 
                android:right="5dp" android:bottom="2dp" /> 
            <corners android:radius="15dp" /> 
        </shape>    
    </item>

    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
            <stroke android:width="2dp" android:color="@color/red" />
            <solid android:color="#FFFFFFFF"/>
            <padding android:left="5dp" android:top="2dp" 
                android:right="5dp" android:bottom="2dp" /> 
            <corners android:radius="15dp" /> 
        </shape>
    </item>

</selector>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜