将图像应用于按钮背景

时间:2013-09-04 06:55:47

标签: android android-layout

我想将图像应用于按钮的背景。

我为它写的风格如下:

 <style name="SingnupButton" parent="@android:style/TextAppearance.Medium">
    <item android:drawable="@drawable/signupButton.jpeg" /> 
    </style>

图片位于我的drawable-hdpi文件夹中。

我将其应用如下:

<Button
            style="@style/SingnupButton"
            android:id="@+id/btn_register"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="@string/btn_register" />

但是此图像未应用于按钮的背景。

我的代码出了什么问题?

请指导我。

5 个答案:

答案 0 :(得分:1)

更改它
<item android:drawable="@drawable/signupButton.jpeg" /> 

<item android:background="@drawable/signupButton" /> 

只需重命名该图片名称并删除.jpeg扩展名

答案 1 :(得分:1)

试试这个:

<style name="SingnupButton" parent="@android:style/TextAppearance.Medium">
    <item android:drawable="@drawable/signupButton" /> 
</style>

答案 2 :(得分:1)

删除

中的.jpg
 <item android:drawable="@drawable/signupButton.jpeg" /> 

答案 3 :(得分:1)

尝试设置按钮的背景并将选择器放到可绘制文件夹中。 不要使用(android:drawable =“@ drawable / button_bg_shape.jpg)任何扩展名,如jpeg,png,gif,...在调用时。尝试在drawable文件夹中使用png文件

XML文件:

       <Button
            android:id="@+id/Bundle_clear"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/selector_button"
            android:text="Clear"
            android:textColor="#FFF"
            android:textSize="@dimen/text_size"
            android:textStyle="bold" />

在drawable文件夹中创建selector_button

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

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

     </selector>

答案 4 :(得分:1)

您在style

中错误地设置了背景

试试这种方式..

<style name="SingnupButton" parent="@android:style/TextAppearance.Medium">
    <item name="android:background">@drawable/signupButton</item>
</style>
相关问题