【问题标题】:Inflate the actionbar from menu in tabhost activity从 tabhost 活动中的菜单膨胀操作栏
【发布时间】:2016-02-11 09:25:04
【问题描述】:

在我的应用程序中,我为 Tabhost 活动中的操作栏充气。即使我使用onCreateOptionsMenu(Menu menu)infolwing 我已经发布了我的代码,这里也不会显示操作栏

TabhostActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="@color/appblue">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"/>
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>
    </LinearLayout>

</TabHost>

这是我的 menuItem.xml(操作栏项目)

<?xml version="1.0" encoding="utf-8"?>
<menu

    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:appmunu="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.after2.svirtzone.after2_gradle.UserDashBoardActivity">
    <item
        android:id="@+id/action_notify"
        android:icon="@drawable/mail_icon"
        appmunu:showAsAction="always"
        android:title="Notification" />
    <item
        android:id="@+id/action_favourite"
        android:icon="@drawable/favourite_icon"
        appmunu:showAsAction="always"
        android:title="Favourite" />

    <item
        android:id ="@+id/action_navigation"
        android:icon="@drawable/ic_menu"
        appmunu:showAsAction = "always"
        android:title="navigation"
        android:layout_gravity="left"/>
</menu>

这是我的 TabhostActivity

public class TabHostActivity extends TabActivity {


    private Context context;
    private static final String TAB_1_TAG = "AboutCollege";
    private static final String TAB_2_TAG = "Focus of Course";
    private static final String TAB_3_TAG = "Admision";
    private static final String TAB_4_TAG = "Contact Details";
    private static final String TAB_5_TAG = "Back";

    @Override
    protected void onStart() {
        super.onStart();
        AppActivityStatus.setActivityStarted();
        AppActivityStatus.setActivityContext(context);
    }

    @Override
    protected void onPause() {
        super.onPause();
        AppActivityStatus.setActivityStoped();

    }

    @Override
    protected void onResume() {
        super.onResume();
        AppActivityStatus.setActivityStarted();
    }

    @Override
    protected void onStop() {
        super.onStop();
        AppActivityStatus.setActivityStoped();
    }
//here this the code for displaying the actionbar
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_user_dash_boad, menu);
        return true;
    }

    // delete the selected event from event list added here
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.action_notify:
                return true;

            case R.id.action_favourite:
                return true;
            case R.id.action_navigation:

        }

        return super.onOptionsItemSelected(item);
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_host);
         context = getApplicationContext();
        TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
        TabHost.TabSpec tag5= tabHost.newTabSpec(TAB_5_TAG);
        TabHost.TabSpec tag4= tabHost.newTabSpec(TAB_4_TAG);
        TabHost.TabSpec tag3= tabHost.newTabSpec(TAB_3_TAG);
        TabHost.TabSpec tag2= tabHost.newTabSpec(TAB_2_TAG);
        TabHost.TabSpec tag1= tabHost.newTabSpec(TAB_1_TAG);
        tag1.setIndicator("", getResources().getDrawable(R.drawable.college_hover)).setContent(new Intent(this,AdmissionActivity.class));
        tag2.setIndicator("", getResources().getDrawable(R.drawable.course)).setContent(new Intent(this, AboutCollegeActivity.class));
        tag3.setIndicator("", getResources().getDrawable(R.drawable.admission)).setContent(new Intent(this, AboutCollegeActivity.class));
        tag4.setIndicator("", getResources().getDrawable(R.drawable.contact)).setContent(new Intent(this, AboutCollegeActivity.class));
        tag5.setIndicator("", getResources().getDrawable(R.drawable.back)).setContent(new Intent(this, AboutCollegeActivity.class));
        tabHost.addTab(tag1);
        tabHost.addTab(tag2);
        tabHost.addTab(tag3);
        tabHost.addTab(tag4);



    }
}

在这里,Actionbar 没有显示,但 TabhostActivity 以外的所有其他活动都运行良好

【问题讨论】:

    标签: android android-actionbar android-tabhost


    【解决方案1】:

    像这样先定义一个appcompatActivity的私有变量

    private AppCompatActivity myact;
    

    然后在 oncreate 方法中添加以下行

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        myact.setSupportActionBar(toolbar);
    

    11级以下的api

    myact.getActionBar(toolbar);
    

    我认为这应该可以解决问题。看到这个问题的根本原因是你的类是扩展 TabActivity 而不是 AppcompatActivity 或 Activity。所以我们必须特别提到是否显示 Actionbar。我建议使用 Android Studio 的标准 Tabsliderview 来设计您的应用程序,它更易于使用且结构良好。

    【讨论】:

    • 如果我添加该行,我在该行得到空指针异常
    • 您使用的是哪个版本的支持库?哪个平台使用的是eclipse或studio?
    • 我正在使用 android studio 支持库 v7
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多