2012-07-31 126 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);