在tabhost活动中从菜单中膨胀操作栏

时间:2016-02-11 09:25:05

标签: android android-actionbar android-tabhost

在我的应用程序中,我在Tabhost活动中对操作栏进行了充气。在这里,即使我使用onCreateOptionsMenu(Menu menu)进行充气,也会显示actionbar dosn。在下面我发布了我的代码

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(Actionbar项目)

<?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 dosn,但TabhostActivity以外的所有其他活动都运行良好

1 个答案:

答案 0 :(得分:0)

首先像这些

定义appcompatActivity的私有变量
private AppCompatActivity myact;

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

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

api低于11级

myact.getActionBar(toolbar);

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

相关问题