如何为TabLayout中的每个Activity设置标题栏图标

5
在我的tablayout示例中,我创建了3个选项卡,并为每个选项卡设置了3个活动。我可以将图像设置为活动的标题栏,从而将意图添加到每个选项卡中。由于这个原因,在所有3个选项卡中都能看到标题栏中的图像。我的要求是为每个活动的标题栏设置不同的图像。我按照这个的方法设置标题栏图像。但是当我尝试对每个活动进行相同的操作时,会出现错误android.util.AndroidRuntimeException: You cannot combine custom titles with other title features,应用程序终止。

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

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

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

<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

3个回答

6

你不能在嵌套在TabHost中的活动中使用自定义标题功能。

也就是说,如果你从嵌套在TabActivity中的Activity AActivity B请求自定义标题,Android将抛出上述异常。

解决此问题的方法是让TabActivity请求自定义标题。然后从Activity A和Activity B中更改TabActivity自定义标题的内容。

我可以给你另一个提示,就是重写Activity A和Activity B的onResume()调用来更改TabActivity自定义标题。

编辑:示例代码

对于你的选项卡活动

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 活动。
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);
}

请在简历上复制ActivityA的内容,并针对B进行修改。

提示:您可以使用此方法从每个活动更改标题布局。

希望这能有所帮助。


@Shardul,感谢您宝贵的建议。实际上,我是Android的新手,不知道如何从我的活动中获取标题属性。如果您有任何示例,请建议我。 - Venugopal
样例已添加。希望这能为您解决所有问题。 - Shardul
@Shardul,非常感谢。你的代码运行得非常好。 - Venugopal
@Shardul,请问我该如何从my_title.xml中获取ImageView的引用?当我直接在ActivityA的onResume()中调用findViewById()时,它返回null值。 - Venugopal
@Venu Gopal,我也遇到了同样的问题。你的ImageView在TabActivity中,所以你必须在那里找到它:TabLayoutDemo.myTabLayoutDemo.findViewById() - Konsumierer
@Shardul:我不确定你是否需要在内部活动中设置该功能。对我来说,在TabActivity中这样做很好用。顺便说一下,你代码中ActivityA中静态变量的声明和访问似乎是错误的? - Konsumierer

4

您可以轻松更改的(标准)标题

  1. implementing onChildTitleChanged in the TabActivity

    @Override 
    protected void onChildTitleChanged(Activity childActivity, CharSequence title) {
        super.onChildTitleChanged(childActivity, title);
        setTitle(title);
    }
    
  2. set the title in the child activities, f.e.

    @Override 
    protected void onResume() {
        setTitle("Calender");
        super.onResume();
    }
    

使用这种策略更改自定义标题不应该很难。例如,您的所有子活动都可以实现一个接口并具有一个方法。

    public int getTitleResource()

选项卡功能现在可以执行以下操作:
    @Override 
    protected void onChildTitleChanged(Activity childActivity, CharSequence title) {
        super.onChildTitleChanged(childActivity, title);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, ((TabChild)childActivity).getTitleResource());
    }

或者您可以将id作为子标题字符串的一部分传输,并将其解析为整数...

0
--------------------------- One more Action for the same --------------------
Step Declare instance of class:
Parent Tab Activity:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.tab_layout_title);
TabLayoutActivity.tabLayout = this;
TextView titleText = (TextView) findViewById(R.id.myTitle);
titleText.setText("Care");

Child Activity
@Override 
    protected void onResume() {
        super.onResume();
        TabLayoutActivity.tabLayout.titleText.setText("Title name");

    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接