繁体   English   中英

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

[英]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来访问TextViewImageView 然后,您显然可以在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.

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