2015-05-09 68 views
1

fragment_camera.xml:的Android按鈕背景形狀:圓壓扁

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

    <ImageView 
     android:id="@+id/iv_camera" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentTop="true" > 
    </ImageView> 

    <Button 
     android:id="@+id/btn_take_pic" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="20dp" 
     android:background="@drawable/circle" > 
    </Button> 

</RelativeLayout> 

circle.xml:

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

    <solid 
     android:color="#87cefa"/> 

    <size 
     android:width="40dp" 
     android:height="40dp"/> 
</shape> 

結果:

enter image description here

我圓壓扁*哭*。

由於按鈕不包含任何內容我猜這個按鈕有一個默認的寬度和高度。我怎樣才能讓按鈕調整到drawable?我是否必須將寬度和高度設置爲與drawable相同的尺寸,還是有動態的方式來做到這一點?

+1

不,你不能。唯一的方法是定義按鈕的尺寸。背景可繪製總是被強制爲視圖的尺寸。 –

+0

@iRaviiVooda好吧,這就是我害怕的,猜想我必須咬緊牙關。 –

回答

-1

別哭了。使用複合可繪製
對於西方人來說,這將是drawableLeft,或者對於中東人來說,這將是drawableRight(這實際上取決於您希望圖像和文字相關的方式 - 甚至可以同時使用它們)。
http://developer.android.com/guide/topics/ui/controls/button.html

+2

ImageButton ftw! –

+0

使用複合繪圖使其看起來偏離中心並且看起來不正確。按照@ KarlMorrison的建議使用ImageButton,或者只是定義按鈕尺寸。 –

+0

@zboarda不,它不。我在我的應用程序中成功使用它。如果它像你說的那樣,OP不會接受我的答案。 –