2013-03-31 154 views
2

我想创建带有background.png和圆角的按钮。这个怎么做?按钮角落圆角背景图片

我写了一篇关于MainActivity此代码:

<Button 
    android:layout_width="match_parent" 
    android:layout_height="40dip" 
    android:text="LOGIN TO THE GAME" 
    android:textColor="#ffffff" 
    android:background="@drawable/button_corners" /> 

而且我创建文件 'button_corners.xml' 包含:

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

    <corners 
     android:radius="10dip" /> 

    <stroke 
     android:width="0.5dp" 
     android:color="#000000" /> 

</shape> 

现在怎么背景图片添加到这个按钮?帮帮我!

回答

0

带圆角和图片的按钮,我从来没有用过。但与背景颜色的按钮,没有任何图像,我用这个代码:

在活动时间:

<Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="20dp" 
    android:text="Ementas" 
    android:background="@drawable/button_corners"/> 

而且对文件 'button_corners.xml':

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

    <corners 
     android:bottomRightRadius="10dp" 
     android:bottomLeftRadius="10dp" 
     android:topLeftRadius="10dp" 
     android:topRightRadius="10dp"/> 

    <gradient 
     android:startColor="@color/green_dark" 
     android:endColor="@color/green_light" 
     android:angle="270" /> 
</shape> 

我仍然有颜色的文件:

<resources> 
    <color name="green_dark">#98B505</color> 
    <color name="green_light">#5F7102</color> 
</resources> 

最后的结果是这样的:

enter image description here

我认为要使用图像,代码不应该太不同。

+1

代码犯规说..where添加背景图片...请甚至包括图像。 。如果可能的话 – bvsss

0

使用线性布局(A)并设置您拥有的任何图像作为背景。然后使用另一个线性布局(B)放置在线性布局(A)内,并为该布局指定一个带圆角的背景。

<LinearLayout 
      android:id="@+id/A" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/whateverimage" 
      android:orientation="vertical" > 

     <LinearLayout 
      android:id="@+id/B" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@drawable/roundedstuff" 
      android:orientation="vertical" > 
</LinearLayout> 
</LinearLayout> 

roundedstuff.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle"> 
<gradient 
    android:startColor="#00000000" 
    android:endColor="#00000000" /> 
<padding android:left="2dp" 
    android:top="2dp" 
    android:right="2dp" 
    android:bottom="2dp" /> 
<stroke 
    android:width="2dp" 
    android:color="#ffffff" /> 
<corners 
    android:bottomRightRadius="10dp" 
    android:radius="10dp" 
    android:bottomLeftRadius="10dp" 
    android:topLeftRadius="10dp" 
    android:topRightRadius="10dp" /> 

保存,要绘制的文件夹