2013-03-19 42 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"

。希望它适合你。