2013-03-19 44 views
4

是否可以製作由多個圖像/比例/對齊組成的xml背景? 所以有一個複雜的可繪製xml可用作android佈局的背景。Android - xml佈局作爲背景

例如,這是動態XML背景下,我想這樣做: enter image description here

所以,我的所有不同的活動,把我上面不同的佈局/視圖: enter image description here

我知道我不能設置一個佈局爲layout_background,但這裏是我想(根據以前的照片)做設計:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/background_image" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" > 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/main_image" /> 
     </RelativeLayout> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="2" > 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="50dp" 
       android:layout_alignParentBottom="true" 
       android:layout_centerHorizontal="true" 
       android:src="@drawable/footer_image" /> 
     </RelativeLayout> 
    </LinearLayout> 

</RelativeLayout> 

我看到我們可以將位圖設置爲可繪製的xml,但是對於scale和alignement? 有沒有更簡單的方法來進行這種視覺渲染?

回答

7

,只要你想在dynamic xml background,並在所有屏幕可以Include背後RelativeLayout喜歡,你可以創建一個佈局這個

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width=」match_parent」 
    android:layout_height=」match_parent」 
    android:background="@color/app_bg" 
    android:gravity="center_horizontal"> 

    <include layout="@layout/my_dynamic_layout"/> 

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    ...Your layout code ... 

    </RelativeLayout > 

</RelativeLayout > 
+2

我認爲另一個相對佈局代替父線性佈局效果更好。 – FabiF 2013-03-19 14:47:15

+0

是的,你是對的@FabiF我編輯了我的答案 – 2013-03-19 14:51:39

+0

完美!非常感謝! RelativeLayout是線索:) – Bibix 2013-03-19 15:07:37

0

我敢肯定你有什麼要求,假設你想放一些XML文件與一個XML文件中的不同圖像我給我的答案。

根據您的要求創建3個佈局,然後創建一個您希望放置這3個佈局的主佈局。

-

  • 例:假設你有

one.xml

two.xml

three.xml

讓我們假設你已經申請根據您的要求選擇所有背景上述佈局

現在

main.xml中

< LinearLayout 
--- 
---> 
< include 
android:layout_width="fill_parent" android:layout_height="wrap_content" 
layout="@layout/one.xml" /> 
< include 
android:layout_width="fill_parent" android:layout_height="wrap_content" 
layout="@layout/two.xml" /> 
< include 
android:layout_width="fill_parent" android:layout_height="wrap_content" 
layout="@layout/3.xml" /> 
< /LinearLayout> 

根據您的要求安排的佈局,如果你需要一些使用百分比重量來劃分這些佈局=」 50"

。希望它適合你。