2013-10-11 41 views
1

我創建了一個menuView.xml佈局,以便放入我的活動的所有佈局中。這種佈局對每個邊界一列這樣一個標題欄:調整佈局以進入其他佈局

ComposeView http://img845.imageshack.us/img845/2121/d6zp.png

我在其他佈局這種方式插入此佈局:

<!-- Show menu --> 
<com.example.MenuView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

但是,如果佈局中的一個具有完全屏幕視圖,這個視圖的一部分被MenuView所覆蓋,所以...

我怎麼能告訴這個視圖,以適應其大小的MenuView中的空白空間不被它覆蓋?

更新 - 完整的XML包括

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


    <!-- Show menu --> 
    <com.example.MenuView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 


    <RelativeLayout 
     android:id="@+id/Left_layout" 
     android:layout_width="fill_parent"   
     android:layout_height="fill_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentBottom="true" > 

    //Here go buttons, views, etc... 

    </RelativeLayout> 

    <RelativeLayout 
     android:id="@+id/Right_layout" 
     android:layout_width="fill_parent"   
     android:layout_height="fill_parent" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentBottom="true" > 

    //Here go buttons, views, etc... 

</RelativeLayout> 

這裏會發生什麼情況是,這些2個相對佈局得到由MenuView(最黑暗的GRE邊框和頂部黑條)覆蓋,理想的方式是將這兩種佈局合併到空白區域(最清晰的灰色)。

我可以解決這個設置邊緣大小的相對佈局,以適應其內部,但我知道這不是最好的方式來做到這一點,所以我不知道是否有另一種方式。

+0

對不起,我的android知識,我找不到更好的方式來做到這一點。我會刪除我的舊答案,因爲它沒用。我希望有人很快發佈答案。 –

+0

好的沒問題,謝謝你的幫助 – masmic

回答

0

我認爲解決問題的最好方法是繼承。 如果您定義的活動可以用作所有充實活動的模板,以便將其內容添加到其中。 我不知道你的自定義菜單「由」,但作爲一個簡單的例子是什麼:

創建代碼的一項基本活動:

public class ActivityWithMenu extends Activity 
    { 
     @Override 
     protected void onCreate(Bundle savedInstanceState) 
     { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_with_menu_layout); 
     } 
    } 

和xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent"  
tools:context=".ActivityWithMenu" > 
<TextView 
    android:layout_width="match_parent" 
    android:layout_height="20dip" 
    android:background="#ff000000" 
    android:textColor="#ffffffff"  
    android:text="Main Menu Title Bar" 
    android:id="@+id/mainmenutitle" /> 
<LinearLayout 
    android:layout_width="20dip" 
    android:layout_height="match_parent" 
    android:layout_below="@+id/mainmenutitle" 
    android:layout_alignParentLeft="true" 
    android:background="#ff999999" 
    android:orientation="vertical" 
    android:id="@+id/lefthandmenu" /> 
<LinearLayout 
    android:layout_width="20dip" 
    android:layout_height="match_parent" 
    android:layout_below="@+id/mainmenutitle" 
    android:layout_alignParentRight="true" 
    android:background="#ff999999" 
    android:orientation="vertical" 
    android:id="@+id/righthandmenu" /> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_toLeftOf="@+id/righthandmenu" 
    android:layout_toRightOf="@+id/lefthandmenu" 
    android:layout_below="@+id/mainmenutitle" 
    android:orientation="vertical" 
    android:id="@+id/activitycontent" 
    /> 
    </RelativeLayout> 

然後爲特定活動創建您的xml,在此情況下爲簡單的「Hello World」佈局:

<?xml version="1.0" encoding="utf-8"?> 
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:scrollbars="vertical" > 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#ff00ff00" 
     android:text="Hello World!" 
     /> 
</LinearLayout> 
    </ScrollView> 

但是現在,當你寫的代碼對於此活動,擴大而不是Activity類直接「ActivityWithMenu」和如下誇大這個XML佈局:

public class Activity1 extends ActivityWithMenu 
    { 
     @Override 
     protected void onCreate(Bundle savedInstanceState) 
     { 
      requestWindowFeature(Window.FEATURE_NO_TITLE); 
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
        WindowManager.LayoutParams.FLAG_FULLSCREEN);  

      super.onCreate(savedInstanceState); 

      LinearLayout ll = (LinearLayout)findViewById(R.id.activitycontent); 

      ScrollView sv = (ScrollView)this.getLayoutInflater().inflate(R.layout.activity1_layout, ll, false); 

      ll.addView(sv); 
     } 
    } 

我加入的代碼,使這裏全屏的活動,而不是在父級ActivityWithMenu類假設你不希望它們全部以這種方式顯示,但是如果適當的話你可以將它移動到父類中。

希望這會有所幫助。

+0

我有一個名爲'menu_view'的佈局,其中我設計了一個菜單,這個菜單由2列與側面的按鈕組成,還有一個自定義標題欄。然後,我也定義了一個名爲'menuView'的類,它定義了所有按鈕的onClickListeners,但是這個類'擴展了RelativeLayout'。在這裏我使用佈局充氣器,然後在其他佈局上插入此菜單,我只是在想要的佈局上定義它。我看到你說擴展這個菜單類作爲一個活動,但是這與我在做什麼兼容? – masmic

+0

我不明白爲什麼不是,經過一段時間的重構後。如果你改變我的'lefthandmenu'/'righthandmenu'的佈局xml爲你的按鈕列和'mainmenutitle'爲你的標題xml,只需保留'toLeftOf'等排列屬性,它應該顯示相同。並將你的onClickListener設置移到父類的onCreate(在我的例子中爲'ActivityWithMenu'),這樣你只需要在一個地方。我認爲這樣做會更容易,而不是試圖將這種複雜的界面控制覆蓋在每個特定活動上。 – Matt

+0

終於嘗試過這種方式,但我有一個問題,它會產生錯誤,並且logcat表示錯誤是在「ActivityWithMenu」佈局膨脹時。它告訴我,它在「ScrollView sv =(ScrollView)this.getLayoutInflater()。inflate(R.layout.activity1_layout,ll,false);」 – masmic