2012-01-19 20 views
2

我正在開發一個Android應用程序。在這個應用程序中,標誌欄顯示在所有頁面上(活動),或者我們可以說它在所有頁面上都有標題。 此徽標欄有幾個圖標,如家庭,登錄,通知等,點擊這些圖標對應的導航將執行。Android是否擁有像.NET或Tiles這樣的MasterPage概念在Struts中爲所有頁面添加標題?

例如,如果用戶在應用程序中的任何地方並且點擊主圖標,他將導航到應用程序的主頁。

我可以通過編碼將logobar.XML充滿我的所有活動。但問題是我必須在徽標欄中的所有圖標的所有頁面上調用onClickListener。 這不是一個好的編程方式。 如何在所有其他活動中實施徽標欄活動而不重複代碼? 機器人有任何主頁的概念,如.net或瓷磚概念在Struts中一樣?

請指導我。

回答

1

編輯:好吧,我明白了。可能是this answer會幫助你。

嘗試使用選項卡小部件與Tabactivity檢查此鏈接使用的片段和選項卡http://developer.android.com/reference/android/app/TabActivity.html爲Android。我認爲對於較低版本我們也可以使用它。這是什麼鏈接說 - 「你可以使用v4支持庫,它提供了一個與DONUT兼容的Fragment API版本。」

+0

感謝您的回覆,但標籤佈局不能解決我的問題。我想將我的徽標欄用作標題欄或標題欄,而不是像徽標欄上的每個圖標使用不同的選項卡。這應該使標題不是標籤的外觀。我正在開發我的應用程序使用android 2.1 –

+0

謝謝AnDro。我認爲這是我想要的,它應該在我的應用程序中工作。 –

1

您必須在xml中創建masterLayout,並且必須將其包含在您必須擁有它的其他 佈局中。

+0

是的我正在這樣做,但問題是如何在主佈局活動的其他活動中使用所有功能(如單擊徽標欄的每個圖標上的事件),其中包括masterLayout而不重複代碼。目前,我必須爲每個頁面上的每個圖標重複一次OnClickListener。 –

+0

@MANOJAGARWAL,因爲您已包含佈局,然後通過該佈局中的id查找視圖並應用您的事件列表。 –

+0

這很好。我已經在使用這種方法。但是,這種方法在所有頁面上重複相同的代碼,因爲我的標誌欄放在所有頁面中。我想要一種方式,讓重複不會發生,我只是在其他活動中致電我的主頁活動。 –

0

我已經完成了這個使用XML文件。

我只是從XML文件創建運行時視圖,並將其添加到活動佈局。對於

public static void setLoginview(Context ctx, RelativeLayout layout) { 
    LayoutInflater linflater = (LayoutInflater) ctx 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View myView = linflater.inflate(R.layout.loginheader, null);  
    layout.addView(myView); 
    try { 
     layout.getChildAt(0).setPadding(0, 50, 0, 0); 
    } catch (Exception e) { 
    } 
} 

CTX

我已經創建方法是應用contetx和佈局是其中我想添加該視圖的佈局。

0

該解決方案非常簡單。

你需要擴展「活動」類,在功能的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);   
     image = (ImageView)findViewById(R.id.ImageView01); 
     image.setImageResource(R.drawable.header); 
     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前需做的唯一的事情。我想這是因爲沒有任何東西可以又調用此函數之前繪製。

這爲我工作的偉大,希望你會發現自己的編碼這個有用。

相關問題