将图像应用于按钮背景



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

我写的风格如下:

 <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" />

但这张图片并没有被应用到按钮的背景上。

我的代码出了什么问题?

请引导我。

您在style中错误地设置了背景。

试试这个方法。。

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

删除中的.jpg

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

尝试设置按钮的背景,并将选择器放在drawable文件夹中。调用时不要使用(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" />

在可提取文件夹中创建选择器按钮(_B)

    <?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>

使用更改

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

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

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

试试这个:

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

最新更新