Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 系统默认设置下的Android自定义应用程序栏_Java_Android_Android Layout - Fatal编程技术网

Java 系统默认设置下的Android自定义应用程序栏

Java 系统默认设置下的Android自定义应用程序栏,java,android,android-layout,Java,Android,Android Layout,我的MainActivity.xml如下所示: <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schema

我的MainActivity.xml如下所示:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
    layout="@layout/app_bar_appbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_appbar"
    app:menu="@menu/activity_appbar_drawer" />

<android.support.design.widget.CoordinatorLayout 
xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/appbar_padding_top"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_weight="1"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:title="@string/app_name">

        </android.support.v7.widget.Toolbar>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/tab_text_1" />

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/tab_text_2" />

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/tab_text_3" />

        </android.support.design.widget.TabLayout>

   </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" >

    </android.support.v4.view.ViewPager>


</android.support.design.widget.CoordinatorLayout>
</android.support.v4.widget.DrawerLayout>
那么,如何让工具栏(ID为“toolbar”)显示,而不是在上图中当前包含文本“AppName”的默认appbar?我也跟着,但是结果没有变化

编辑1:以下屏幕截图显示,出于某种原因,我的用户定义的appbar显示在默认应用程序下方。我还包括了我的app_bar_appbar.xml


一切看起来都是完美的。如果您只想更改工具栏的标题,那么只需在MainActivity.java中添加几行代码即可

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// add few lines here like...
    toolbar.setTitle("Your title here");
// ....   
    setSupportActionBar(toolbar);
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {


       return false;
    }

    return super.onOptionsItemSelected(item);
}
如果右选项菜单未显示,则覆盖MainActivity.java中的这两个方法

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// add few lines here like...
    toolbar.setTitle("Your title here");
// ....   
    setSupportActionBar(toolbar);
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {


       return false;
    }

    return super.onOptionsItemSelected(item);
}

当您将
主题.AppCompat.Light.darkaActionBar
添加到AndroidManifest的
应用程序
标记时,所有活动都将包括默认的actionbar

使用
NoActionBar
在styles.xml中再创建一个主题,并将其应用于
NavigationDrawerActivity

<style name="navigation_theme" parent="Theme.AppCompat.Light.NoActionBar"/>
其中fragment是包含TabLayout和ViewPager的其他片段。

首先从布局预览中选择“AppTheme”

选择项目主题>>AppTheme.NoActionBar

为活动创建选项菜单的步骤

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch(item.getItemId()){

        case R.id.menu_item:
          //do need full stuff
        break;

        case R.id.menu_settings:
          //do need full stuff
        break;

    }

}
在res>menu文件夹中创建menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/menu_item"
        android:title="Item"/>

    <item
        android:id="@+id/menu_settings"
        android:title="Settings" />
</menu>


您是否只需要一个包含所有选项的工具栏(抽屉图标和右侧的其他菜单)?请在您的问题中添加此
@layout/app\u bar\u appbar
布局?还有,屏幕是否符合您的期望?@AmrutBidri我现在已经添加了布局。布局如Dipali的解决方案所示-导航栏位于左侧,选项栏位于右侧。目前,默认情况下它被屏蔽,可能也是由于当前的选项卡视图。我遵循了这一点,但是它只成功地恢复了配色方案。出于某种原因,默认工具栏似乎出现在我的顶部。我将用截图编辑OP。@Talisman,您是否仍能在预览中看到此
NoActionBar主题到活动的默认工具栏?否,我有渲染问题“在当前主题中找不到样式“toolbarStyle”表格布局形成了3个不同页面之间的拆分,然后导航抽屉应该从左边滑入,选项菜单在右上角(如现有答案所示)。理想情况下,TabLayout就在appbar的下方。对不起,是的,这是一个单独的活动,3页是单独的片段。
<activity
            android:name=".NavigationDrawerActivity"
            android:label="@string/title_activity_navigation_drawer"
            android:theme="@style/navigationtheme" />
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.and.NavigationDrawerActivity">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@android:color/holo_blue_light"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </android.support.design.widget.AppBarLayout>

        <!--Your content in this FrameLayout-->
        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            tools:context="com.and.NavigationDrawerActivity">

        </FrameLayout>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            app:srcCompat="@android:drawable/ic_dialog_email" />

    </android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_navigation_drawer"
        app:menu="@menu/activity_navigation_drawer_drawer" />

</android.support.v4.widget.DrawerLayout>
 FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction tr = fm.beginTransaction();
        tr.replace(R.id.frame_container, fragment);
        tr.commitAllowingStateLoss();
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch(item.getItemId()){

        case R.id.menu_item:
          //do need full stuff
        break;

        case R.id.menu_settings:
          //do need full stuff
        break;

    }

}
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/menu_item"
        android:title="Item"/>

    <item
        android:id="@+id/menu_settings"
        android:title="Settings" />
</menu>