简体   繁体   English

选项卡活动中的自定义标题栏上的findViewById

[英]findViewById on Custom Title Bar within Tab Activity

I have a custom title bar set on my TabActivity. 我在TabActivity上设置了自定义标题栏。 The custom title bar contains a TextView and an ImageView in it. 自定义标题栏包含一个TextView和一个ImageView。 The tabHost has multiple tabs. tabHost具有多个选项卡。

What I want to do is to access the ImageView resource in the tabs. 我要做的是访问选项卡中的ImageView资源。

I am accessing the TextView from custom title bar in the main Tab activity (that extends TabActivity) and it is working fine. 我正在主Tab活动(扩展TabActivity)中的自定义标题栏中访问TextView,并且工作正常。 Following is the code which is working fine in main activity: 以下是在主要活动中运行良好的代码:

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);

Now I want to access the ImageView in my tabs (which are added in tabHost). 现在,我想访问我的选项卡(已添加到tabHost中)中的ImageView。 If I set following code in tab: 如果我在标签中设置以下代码:

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

It gives following error: 它给出以下错误:

You cannot combine custom titles with other title features

And if i directly set following in the tab code: 如果我直接在标签代码中设置以下内容:

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

The image remains null. 图像保持为空。

mycustomtitle.xml has following two elements: 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>

Please give me some idea how can I access the Custom title's ImageView in the tabs ? 请给我一些想法,如何在选项卡中访问自定义标题的ImageView?

You can access the TextView and ImageView by declaring them public static in the TabActivity . 您可以通过在TabActivity中将它们声明为public static来访问TextViewImageView Then, in the Sub-Activity you obviously access the public static TextView and ImageView like, 然后,您显然可以在Sub-Activity访问公共静态TextView和ImageView,例如,

Main_Tab_Activity.textView.setText("my_text_view");

If you put Custom Title bar only in Main TabActivity that Title bar will appears in all of your Sub-TabActivity. 如果仅将自定义标题栏放在主TabActivity中,则标题栏将出现在所有Sub-TabActivity中。

For Example if you gave Custom title Bar in Main 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);

there is no need to give in all you Sub-tab Activity. 您无需将所有“子标签”活动都包括在内。

In all sub-Tab Activity you just set setContentView(R.layout.sub_layout); 在所有子选项卡活动中,只需设置setContentView(R.layout.sub_layout);

use 采用

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

insted id findViewById(id); 插入的ID findViewById(id); for access any view of parent. 用于访问任何父视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM