[英]findViewById on Custom Title Bar within Tab Activity
我在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);
現在,我想訪問我的選項卡(已添加到tabHost中)中的ImageView。 如果我在標簽中設置以下代碼:
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?
您可以通過在TabActivity
中將它們聲明為public static
來訪問TextView
和ImageView
。 然后,您顯然可以在Sub-Activity
訪問公共靜態TextView和ImageView,例如,
Main_Tab_Activity.textView.setText("my_text_view");
如果僅將自定義標題欄放在主TabActivity中,則標題欄將出現在所有Sub-TabActivity中。
例如,如果您在Main TabActivity中提供了Custom title Bar:
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);
采用
getParent().getWindow().findViewById(id)
插入的ID findViewById(id);
用於訪問任何父視圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.