2015-05-09 65 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不会接受我的答案。 –