【问题标题】:Setting rounded corners and image to a button将圆角和图像设置为按钮
【发布时间】:2012-08-10 10:49:21
【问题描述】:

我正在尝试使按钮的角变圆,并设置背景图像。虽然我能够实现圆角按钮,但不能将图像设置为背景。当我尝试android:drawableTop:@drawable/filename 时,按钮再次转换为其默认形状,文本位于图像下方,但我希望图像作为背景,那么如何将东西(圆角和背景图像)应用到按钮?

我在drawable中创建了一个名为button_shape的xml文件;它的代码是:

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

android:shape="rectangle"
>

<solid android:color="#ffffff"
     />

<corners
    android:bottomLeftRadius="20dp"
    android:bottomRightRadius="20dp"
    android:topLeftRadius="20dp"
    android:topRightRadius="20dp" />
<stroke android:width="3dp" android:color="#000000" />
</shape>

我放置按钮的主要xml文件代码是:

<Button
    android:id="@+id/button1"
    android:layout_width="150dp"
    android:layout_height="40dp"
    android:layout_marginTop="179dp"
    android:background="@drawable/button_shape"
    android:text="Button"
    android:textColor="#000000" 
    />

请帮帮我。

【问题讨论】:

  • 嗨,我知道你在清理项目时指定的内容:)

标签: android android-layout android-button


【解决方案1】:

在形状上使用这个

solid android:color="@android:color/transparent" />

和 imageview 应该是

<ImageView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"  android:scaleType="fitXY"   android:background="@drawable/shape"     
     android:src="@drawable/yourimage" />

【讨论】:

  • 嘿thanx 的回复,但我使用的是按钮而不是 imageview .... 你能告诉我一个按钮吗???
【解决方案2】:

使用此 background.xml 放入 Drowable 并设置在 Button android:background="@drawable/ background" 和 ovel_shape 是可绘制的 xml 文件,您可以在其中放置圆角的代码

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

    <item
        android:id="@+id/rounded_corn"
        android:drawable="@drawable/ovel_shape"/>
    <item
        android:id="@+id/background_img"
        android:drawable="@drawable/image"/>

</layer-list>

【讨论】:

    猜你喜欢
    • 2013-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-14
    • 2011-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多