【问题标题】:Button Corners Rounded Background Image按钮角圆角背景图像
【发布时间】:2013-03-31 16:07:42
【问题描述】:

我想创建带有 background.png 和圆角的 Button。如何做到这一点?

我在 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>

现在如何给这个按钮添加背景图片?救命!

【问题讨论】:

    标签: android image button background rounding


    【解决方案1】:

    带有圆角和图片的按钮,我从未使用过。但是对于带有颜色背景的按钮,没有任何图像,我使用了以下代码:

    在活动中:

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

    最终结果是这样的:

    我觉得用图片,代码应该不会差太多。

    【讨论】:

    • 你的代码没有说..在哪里添加背景图片...请包括图片..如果可能的话
    【解决方案2】:

    使用线性布局(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" />
    

    保存到drawable文件夹

    【讨论】:

      猜你喜欢
      • 2012-06-16
      • 2013-10-07
      • 2011-03-22
      • 2017-03-10
      • 1970-01-01
      • 2014-05-11
      • 2020-05-11
      • 2017-01-14
      • 2016-03-18
      相关资源
      最近更新 更多