簡體   English   中英

將圓角和圖像設置為按鈕

[英]Setting rounded corners and image to a button

我試圖使button'corners變圓,以及設置背景圖像。 雖然我能夠實現帶有圓角的按鈕,但是無法將圖像設置為背景。 當我嘗試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" 
    />

請幫我解決一下這個。

使用它的形狀

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

使用此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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM