2010-01-28 29 views
6

我想在每個活動頁腳上放置一個共同的橫幅和菜單。如何在Android中實現類似Activity的主頁和子頁面?

任何人都可以指導我如何在Android中實現類似asp.net的主頁和子頁面?

任何幫助,將不勝感激。

+0

還有就是如何在Android的做一個 「母版頁」 佈局一個簡短的教程 - [這裏](http://jnastase.alner.net/archive/2011/ 1月8日/ ldquomaster-pagesrdquo合android.aspx)。 –

回答

8

您可以讓每項活動擴展一個通用基類,該基類有一個onCreateOptionsMenu方法,該方法每次都從相同的XML擴充菜單。儘管你不能有多重繼承,但是當你想要有簡單的活動和列表活動時,這可能會非常棘手。

另一種方法是讓Util類擁有類似setupMenu(Menu)的方法,如果您正在執行更復雜的菜單設置,則每個活動都可以調用該方法。

根據您的每項活動的XML UI佈局,您可以使用<include/>標籤包含一個通用橫幅。

+0

要實施commomn橫幅可以請你提供我的例子???? – UMAR

+0

我在答案中的鏈接有一個例子。 Android主屏幕應用程序通過使用標籤多次包含workspace_screen.xml的整個佈局。 –

0

我有同樣的問題,並使用ActivityGroup解決它。 我想菜單項會將用戶移動到另一個活動,因此在每個活動中使用BACK按鈕關閉應用程序中的相同菜單幾乎是不可能的(在一段時間之後用戶將不得不返回他所見過的所有活動)。

我還沒有發現任何英語好的教程等等都前段時間寫的礦(這有點太短了,只有用波蘭語,但谷歌Tranlslated版本應該是可以理解的)檢查this

你也可以檢查如何TabHost works

1

該解決方案非常簡單。

你需要擴展「活動」類,在功能的onCreate到的setContentView你的基地XML佈局,也需要覆蓋的setContentView在基地活動課

例如:

1,創建「base_layout .XML」用下面的代碼

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" 
     android:background="#000000" 
     android:padding="15dp" > 
    <LinearLayout android:orientation="horizontal" android:background="#000000" 
      android:layout_width="fill_parent" android:layout_height="wrap_content" 
      android:minHeight="50dp" android:paddingLeft="10dp"> 
      <ImageView android:layout_width="wrap_content" android:id="@+id/ImageView01" 
       android:adjustViewBounds="true" android:layout_height="wrap_content" 
       android:scaleType="fitCenter" android:maxHeight="50dp" /> 
    </LinearLayout> 
    <LinearLayout android:id="@+id/linBase" 
    android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 
    </LinearLayout> 
</LinearLayout>  

2.創建 「BaseActivity.java」

public class BaseActivity extends Activity { 
    ImageView image; 
    LinearLayout linBase;  
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     super.setContentView(R.layout.base_layout);   
     linBase = (LinearLayout)findViewById(R.id.linBase); 
    } 
    @Override 
    public void setContentView(int id) { 
     LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     inflater.inflate(id, linBase); 
    } 
} 

public class SomeActivity extends BaseActivity {  
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     super.setContentView(R.layout.some_layout); 
     //rest of code 
    } 
} 

我注意到,到目前爲止是請求進度條時(requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS)),這需要調用super.onCreate前需做的唯一的事情。我認爲這是因爲在調用這個函數之前還沒有繪製任何東西。

這對我很好,希望你會發現這對你自己的編碼有用。

0

ViewStub是解決方案

activity_masterpage。XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" >  
<ViewStub android:id="@+id/stub_content" 
       android:inflatedId="@+id/subTree" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" /> 

stub = (ViewStub) findViewById(R.id.stub_content); 
stub.setLayoutResource(R.layout.content_layout); 
stub.inflate(); 
相關問題