2012-07-31 174 views
2

我有一个RelativeLayout与2 Button s里面。如何设置图像RelativeLayout背景

一个Buttonlayout_alignParentLeft="true"财产和其他按钮有layout_alignParentRight="true"

我想设置我的RelativeLayout的背景,显示在中间的图像。我有一个play.9.png在我的绘制和使用下面的代码:

<RelativeLayout 
    android:id="@+id/relativeLayout12" 
    android:layout_width="fill_parent" 
    android:layout_height="70dp" 
    android:background="@drawable/play" // background line 
> 

    <Button 
     android:id="@+id/bAbout" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:text="but1" /> 

    <Button 
     android:id="@+id/bSync" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:text="but2" /> 
</RelativeLayout> 

但是当我使用的背景线,我Button小号想念自己的位置

见下 enter image description here

的形象,但我希望我的RelativeLayout是这样的:

enter image description here

什么是错的?

+0

请问您可以上传'play.9.png'图片吗?即使你的代码对我来说工作正常 – sunil 2012-07-31 11:20:20

回答

4

最后我做到这一点的家伙 我用这个代码:

<RelativeLayout 
    android:id="@+id/relativeLayout12" 
    android:layout_width="fill_parent" 
    android:layout_height="70dp" 
    android:background="@drawable/play" // background line 
> 

    <Button 
     android:id="@+id/bAbout" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:text="but1" /> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:scaleType="fitXY" 
     android:src="@drawable/play" /> 

    <Button 
     android:id="@+id/bSync" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:text="but2" /> 
</RelativeLayout> 

,我的结果是这样的:

enter image description here

和工作原理。谢谢大家

0
<RelativeLayout 
android:id="@+id/relativeLayout12" 
android:layout_width="fill_parent" 
android:layout_height="70dp" 
android:background="@drawable/play" // background line 
> 

<Button 
    android:id="@+id/bAbout" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:text="but1" /> 

<Button 
    android:id="@+id/bSync" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" 
    android:text="but2" /> 
</RelativeLayout> 
+0

不,它不工作。不幸的是我的按钮水平位置也错过了 – aTa 2012-07-31 10:57:18

+0

尝试给按钮布局高度70dp – 2012-07-31 11:01:16

+0

我这样做,但不工作 – aTa 2012-07-31 11:07:46

0

我想你应该从两个按钮删除

android:layout_alignParentBottom="true" 
android:layout_alignParentTop="true" 

,并设置按钮高度 'FILL_PARENT'。

+0

我这样做,但这也不工作 – aTa 2012-07-31 11:05:05

0

试试这个。

RelativeLayout rLayout = (RelativeLayout) findViewById (R.id.rLayout); 
    Resources res = getResources(); 
    Drawable drawable = res.getDrawable(R.drawable.newImage);added to the res folder 

    rLayout.setBackground(drawable);