2011-09-20 25 views
4

在我的應用程序中,我想繪製背景圖像的頂部。我有以下xml:如何android Z命令?

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/bg2" 
> 
    <com.myapp.drawings.DrawingSurface 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:id="@+id/drawingSurface" 
    /> 
    <LinearLayout 
      android:orientation="horizontal" 
      android:background="@drawable/bg2" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 
     <Button 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:text="OK" 
       android:onClick="onClick" 
       android:id="@+id/colorBlueBtn" 
     /> 
     <Button 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:text="Save" 
       android:onClick="onClick" 
       android:id="@+id/saveBtn" 
     /> 
    </LinearLayout> 
</FrameLayout> 

不,問題是,當我嘗試在繪圖表面上繪圖時未顯示繪圖。顯示背景圖像和按鈕。一旦我保存了它,就會顯示我的應用程序生成的圖像文件。我認爲問題在於我的佈局的Z順序。

任何想法?謝謝你的幫助! :)

回答

8

首先在xml中顯示的項目將首先繪製。所以你的表面視圖就在你的線性佈局之下。

5

根據Android Developers' description of FrameLayout

孩子的意見被描繪在一個堆棧,頂部最近添加的孩子 。

所以,在你的XML中,LinearLayout是最後繪製的,而且因爲它有match_parent屬性,它完全隱藏您的繪圖表面。

所以,儘量使用RelativeLayout,並設置LinearLayout屬性只是wrap_content,這樣的事情:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/bg2" 
> 
    <com.myapp.drawings.DrawingSurface 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:id="@+id/drawingSurface" 
    /> 
    <LinearLayout 
      android:orientation="horizontal" 
      android:background="@drawable/bg2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true"> 
     <Button 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:text="OK" 
       android:onClick="onClick" 
       android:id="@+id/colorBlueBtn" 
     /> 
     <Button 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:text="Save" 
       android:onClick="onClick" 
       android:id="@+id/saveBtn" 
     /> 
    </LinearLayout> 
</RelativeLayout> 

你也可以完全離開了LinearLayout,只是設置按鈕屬性,留在底部等。