如何设置图标,标题栏为TabLayout每个活动 [英] How to set icon to title bar for each Activity in TabLayout

查看:121
本文介绍了如何设置图标,标题栏为TabLayout每个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我tablayout例子中,我创建了3个标签,因为通常我设置3活动,为每个标签。我可以设置图像的活动标题栏,它增加了意图,每个选项卡。由于这个原因,在标题栏图像中的所有3个标签可见。我的要求是设置​​一个不同的图像,为每个活动标题栏。我跟着这个来设置图片的标题栏。但是,当我会做同样的事情,每一项活动,让 android.util.AndroidRuntimeException:不能将自定义标题与其他标题功能这个错误,应用程序被终止。

In my tablayout example, i have created 3 tabs, as usually i set 3 activities for each tab. I can set image to title bar of activity, which adds the intent to each tab. Due to this, the image in the title bar is visible in all 3 tabs. My requirement is to set a different image to title bar for each activity. I followed this to set image to title bar. But when i am going to do the same thing to each activity, getting android.util.AndroidRuntimeException: You cannot combine custom titles with other title features this error and application is terminated.

manifest.xml的

manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.aptitsolution.tablayout"
  android:versionCode="1"
  android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/MyTheme">
    <activity android:name=".TabLayoutDemo"
              android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

 <activity android:name="AlbumsActivity"></activity>

  

TabLayoutDemo.java

TabLayoutDemo.java

public class TabLayoutDemo extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.main);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.my_title);

    Resources res = getResources(); 
    TabHost tabHost = getTabHost(); 
    TabHost.TabSpec spec;  
    Intent intent;  

    intent = new Intent().setClass(this, ArtistsActivity.class);
    spec = tabHost.newTabSpec("artists").setIndicator("Artists",
                      res.getDrawable(R.drawable.ic_tab_artists))
                  .setContent(intent);
    tabHost.addTab(spec);
    ....
    ....

ArtistsActivity.java

ArtistsActivity.java

public class ArtistsActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);//i am getting the error here     
    setContentView(R.layout.artists);
    setFeatureDrawableResource(Window.FEATURE_CUSTOM_TITLE, R.layout.my_title);     

}

}

my_title.xml

my_title.xml

<RelativeLayout android:id="@+id/header"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="fill_parent">
<ImageView android:src="@drawable/nowplaying"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView android:id="@+id/title" android:layout_width="wrap_content"
    android:text="New Title" android:layout_height="wrap_content"/></RelativeLayout>

谢谢
VENU

thanks
venu

推荐答案

您好,您不能使用自定义标题从嵌套在该活动的特征 TabHost

Hello You cannot use Custom Title feature from activities that are nested in TabHost.

也就是说,如果你是从申请自定义标题的活动A b活动的嵌套在 TabActivity Android将引发异常,你提到的上方。

That is if you are requesting Custom Title from Activity A and Activity B which are nested in TabActivity Android will throw the exception you mentioned above.

解决这个问题是让TabActivity要求自定义标题。而从活动A和活动B的内部改变TabActivity的自定义标题内容。

Work around to this issue is to let TabActivity request custom title. And change content of Custom Title of TabActivity from inside the Activity A and Activity B.

另一个秘诀,我可以给你的是覆盖活动A和活动B的 onResume()呼吁改变TabActivity自定义标题。

Another tip I can give you is override the onResume() call of Activity A and Activity B to change the TabActivity custom title.

编辑:样品code

有关你的选项卡的活动。

For your tab activity

public class TabLayoutDemo extends TabActivity {

//CREATING A PUBLIC STATIC VARIABLE
public static TabLayoutDemoInstance myTabLayoutDemo;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.main);

    TabLayoutDemo.TabLayoutDemoInstance=this;//STORING 

    //COMMENTING SET FEATURE IN TAB
    //getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.my_title);
//REST CODE REMAINS SAME

现在为您的活动A和B

Now for your Activity A and B

public class ActivityA extends Activity{
//LOST MORE CODE ABOVE
@Override
protected void onResume() {
    super.onResume();
//SET FEATURE FROM INSIDE ACTIVITY
    TabLayoutDemo.TabLayoutDemoInstance.getWindow().
           setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.my_title);
}

}

复制上面指定的B来说ActivityA的简历。

Copy on resume of ActivityA specified above for B as well.

提示:你可以改变使用这个从每个活动更改标题布局

Tip: you can change change the title layout from each activity using this.

我希望帮助。

这篇关于如何设置图标,标题栏为TabLayout每个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆