【问题标题】:How to set icon to title bar for each Activity in TabLayout如何为 TabLayout 中的每个 Activity 设置图标到标题栏
【发布时间】:2010-12-26 12:36:56
【问题描述】:

在我的选项卡布局示例中,我创建了 3 个选项卡,通常我为每个选项卡设置 3 个活动。我可以将图像设置为活动的标题栏,从而将意图添加到每个选项卡。因此,标题栏中的图像在所有 3 个选项卡中都可见。我的要求是为每个活动的标题栏设置不同的图像。我跟着this 将图像设置为标题栏。但是当我要对每个活动做同样的事情时,得到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>

谢谢
维纽

【问题讨论】:

    标签: android


    【解决方案1】:

    您好,您不能使用嵌套在 TabHost 中的活动中的 Custom Title 功能。

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

    解决此问题的方法是让 TabActivity 请求自定义标题。并从 Activity A 和 Activity B 内部更改 TabActivity 的 Custom Title 内容。

    我可以给你的另一个提示是覆盖 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);
    }
    

    }

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

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

    希望对你有帮助。

    【讨论】:

    • @Shardul,感谢您的宝贵建议。实际上,我是 android 新手,我不知道如何从我的活动中获取标题属性。如果你有任何例子,请给我建议。
    • 已添加示例。希望这能为您解决一切问题。
    • @Shardul,非常感谢。您的代码运行良好。
    • @Shardul,你能告诉我如何从 my_title.xml 获取 imageview 的引用吗?当我直接从 ActivityA 的 onResume() 调用 findViewById() 时,它给出的是 null 值。
    • @Venu Gopal,我遇到了同样的问题。您的 imageview 在 TabActivity 中,因此您必须在那里找到它:TabLayoutDemo.myTabLayoutDemo.findViewById()
    【解决方案2】:

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

    1. 在 TabActivity 中实现 onChildTitleChanged

      @Override 
      protected void onChildTitleChanged(Activity childActivity, CharSequence title) {
          super.onChildTitleChanged(childActivity, title);
          setTitle(title);
      }
      
    2. 在子活动中设置标题,f.e.

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

    使用此策略更改自定义标题应该不会那么难。 F.e.你所有的子活动都可以实现一个接口并有一个方法

        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 作为子标题字符串的一部分传输并将其解析回 int...

    【讨论】:

      【解决方案3】:
      --------------------------- 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");
      
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-03-17
        • 2023-03-26
        • 1970-01-01
        • 2011-05-02
        • 1970-01-01
        • 2016-03-25
        • 2014-05-09
        相关资源
        最近更新 更多