2014-12-25 130 views
1

我想做一輪Android ImageButton。爲此,我根據link編寫以下代碼。ImageButton背景與XML

在我的main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dp" 
     android:layout_gravity="center" 
     android:textColor="#333" 
     android:textSize="18sp" 
     android:text="Invite Activity." /> 

    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/imageButton" 
     android:src="@drawable/round" 
     android:background="@drawable/roundcorner" 
     android:padding="50dp" /> 

</LinearLayout> 

在我RES /繪製/ roundcorner.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid android:color="#33DDFF" /> 
    <corners android:radius="100dp" /> 
</shape> 

但代碼不能正常工作。輸出就像下面 Image1

然而,當我在main.xml中

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/imageButton" 
    android:src="@drawable/round" 
    android:background="@drawable/roundcorner" 
    android:padding="50dp" /> 

改變的ImageButton的ImageView它的工作原理如下

Image2

我我感到困惑,因爲我知道ImageButton繼承它來自ImageView。爲什麼它的工作方式不同?有沒有什麼辦法解決這一問題?提前致謝!

+0

你可以發佈完整的XML ... –

+0

我已更新完整的xml – pp31630

回答

0

將src設置爲android:src =「@ drawable/roundcorner」,或者將其設置爲透明,以便您可以看到背後的圓形背景。

+0

結果就像[this](http://imgur.com/YQMtoKy)。所以看起來大圖像掩蓋了按鈕......有沒有辦法讓圖像適合圓形按鈕(裁剪圖像以適合圓形按鈕)? – pp31630

+0

選中此項,是否需要與圖像查看輸出相同的圖像。 <的ImageButton 機器人:ID = 「@ + ID/ImageButton的」 機器人:layout_width = 「WRAP_CONTENT」 機器人:layout_height = 「WRAP_CONTENT」 機器人:背景= 「@繪製/ roundcorner」 機器人:scaleType = 「fitCenter」 android:src =「@ drawable/round」/> – Sunit2k15