2011-10-14 144 views
2

內的自定義標題欄上的findViewById我在我的TabActivity上設置了一個自定義標題欄。自定義標題欄包含一個TextView和一個ImageView。 tabHost有多個選項卡。在標籤活動

我想要做的是訪問標籤中的ImageView資源。

我正在從主Tab選項(它擴展TabActivity)的自定義標題欄訪問TextView,它工作正常。以下是在主要活動中正常工作的代碼:

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
setContentView(R.layout.myactivity); 
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mycustomtitle); 
TextView tv = (TextView) findViewById(R.id.viewTitleId); 

現在我想訪問我的選項卡中的ImageView(它們被添加到tabHost中)。如果我設置在標籤下面的代碼:

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 

它提供了以下錯誤:

You cannot combine custom titles with other title features 

如果我直接設置下面的標籤代碼:

getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mycustomtitle); 
ImageView image = (ImageView) findViewById(R.id.imageTitleId); 

的圖像保持爲空。

mycustomtitle.xml有以下兩個要素:

<TextView 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.80" 
    android:id="@+id/viewTitleId" 
    android:textColor="@color/titleBarTextColor" 
    android:gravity="center_vertical" 
    android:text="@string/viewText" 
    android:textSize="18px" 
    android:paddingLeft="45px" 
    /> 

<ImageView 
    android:layout_height="wrap_content" 
    android:layout_width="0dp" 
      android:layout_weight="0.20" 
    android:src="@drawable/btn_save" 
    android:id="@+id/imageTitleId" 
    > 
</ImageView> 

請給我一些想法,我怎麼能在標籤訪問自定義標題的ImageView的?

回答

4

您可以通過在TabActivity宣佈他們public static訪問TextViewImageView。然後,在Sub-Activity你明明訪問公共靜態的TextView和ImageView的喜歡,

Main_Tab_Activity.textView.setText("my_text_view"); 
+0

這對我有用:)謝謝Lalit – rizzz86

+2

這真的是一個糟糕的建議。請停止爲SO用戶創建內存泄漏。 – Reno

+2

嗨雷諾心靈知道爲什麼有內存泄漏? – ericlee

2

如果您只將自定義標題欄放在主TabActivity中,則標題欄將出現在您的所有子TabActivity中。

例如,如果你給在主要TabActivity自定義標題欄:

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
setContentView(R.layout.main_layout); 
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.header_layout); 
TextView tv = (TextView) findViewById(R.id.viewTitleId); 
ImageView image = (ImageView)findViewById(R.id.imageTitleId); 

沒有必要在你的子標籤活動給予。

在所有子選項卡活動您只需設置setContentView(R.layout.sub_layout);

+0

是@Venky我們可以在主tabactivity中訪問textview和imageview。但我想在我的子選項卡活動中訪問ImageView。我想要在每個子選項卡上的ImageView的不同行爲。 – rizzz86

+0

@ rizzz86爲什麼你需要在子活動中使用該圖像?默認情況下,它會出現在所有的子活動權? – Venky

+0

是的,它出現在所有的子活動中。但是當我點擊ImageView時,我需要在每個子選項卡上有不同的行爲。例如,當第一次打開選項卡點擊ImageView時,它將存儲在table1中的值,如果點擊第二次打開選項卡的ImageView它將存儲在table2等值 – rizzz86

1

使用

getParent().getWindow().findViewById(id) 

insted的ID findViewById(id); 訪問父的任何觀點。