Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.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
Android 手机布局使用SupportToolbar,平板电脑布局使用独立操作栏_Android_Android Layout_Material Design_Tablet_Screen Orientation - Fatal编程技术网

Android 手机布局使用SupportToolbar,平板电脑布局使用独立操作栏

Android 手机布局使用SupportToolbar,平板电脑布局使用独立操作栏,android,android-layout,material-design,tablet,screen-orientation,Android,Android Layout,Material Design,Tablet,Screen Orientation,今天我在想办法改进我的平板电脑设计,我发现了这张图片 我非常想要它,因为它看起来很神奇。我在谷歌搜索了大约一个小时,还没有找到任何好的教程。我找到了这个: 我马上就开始实施,但我尝试的一切都完全错了。独立操作栏的主题需要是ThemeOverlay.AppCompat.actionbar,但是在我的手机布局上,我正在扩展主题.AppCompat.NoActionBar主题。(主题如下) 只是不清楚我应该怎么做才能在平板电脑上制作出上面的图片,并在手机上显示正常(定制)supportActionB

今天我在想办法改进我的平板电脑设计,我发现了这张图片

我非常想要它,因为它看起来很神奇。我在谷歌搜索了大约一个小时,还没有找到任何好的教程。我找到了这个:

我马上就开始实施,但我尝试的一切都完全错了。独立操作栏的主题需要是
ThemeOverlay.AppCompat.actionbar
,但是在我的手机布局上,我正在扩展
主题.AppCompat.NoActionBar
主题。(主题如下)

只是不清楚我应该怎么做才能在平板电脑上制作出上面的图片,并在手机上显示正常(定制)supportActionBar,而不会弄乱其中一个

以下是我的AppTheme样式(我应用于我的应用程序)


@颜色/原色
@颜色/原色暗
@颜色/颜色重音
在你提问之前,是的,我发现问题是这样的,但不,这不是一个重复的问题。克里斯·贝恩斯写的帖子也没有让我明白

是否可以在不破坏两个布局的情况下执行此操作?大声想想,我选择自定义工具栏的原因是因为我包含了一个自定义的searchview,但删除了它。工具栏中还有另一个视图,但我认为如果确实有必要,可以将其删除

这是我手机版本的布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:theme="@style/Theme.AppCompat.NoActionBar">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:animateLayoutChanges="true"
        android:orientation="vertical">

        <include android:id="@+id/toolbar" layout="@layout/toolbar"
            app:layout_scrollFlags="scroll|enterAlways" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/toolbar"
            android:background="@color/primaryColor"
            android:minHeight="?attr/actionBarSize"/>

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

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/appBarLayout"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity" />

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <FrameLayout
                android:id="@+id/replaceFrameLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/errorWarnings"
                    android:visibility="gone"
                    android:gravity="center"
                    android:layout_gravity="center">

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/errorIcon"
                        android:tint="@color/fab_material_red_500"
                        android:layout_centerVertical="true"/>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/errorDescription"
                        android:layout_below="@+id/errorIcon"/>

                </RelativeLayout>

            <com.tim.koers.wallpapers.UI.FilterButton
                android:id="@+id/filterButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right|bottom"
                android:layout_margin="16dp"
                android:clickable="true"
                android:src="@drawable/ic_menu_filter"
                android:elevation="6dp"
                android:tint="@color/fab_material_white"
                android:visibility="gone"/>

        </FrameLayout>

        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:menu="@menu/drawer"/>

    </android.support.v4.widget.DrawerLayout>

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

以下是我的平板电脑布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v4.widget.DrawerLayout
            android:id="@+id/drawerLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:fitsSystemWindows="true">

            <android.support.design.widget.AppBarLayout
                android:id="@+id/appBarLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:animateLayoutChanges="true"
                android:orientation="vertical"
                android:background="@android:color/white"
                android:layout_marginEnd="64dp"
                android:layout_marginStart="64dp"
                android:layout_marginTop="56dp">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <!-- This is the secondary toolbar, 72dp also according to specs -->
                    <include android:id="@+id/toolbar" layout="@layout/toolbar"
                        app:layout_scrollFlags="scroll|enterAlways" />

                    <android.support.design.widget.TabLayout
                        android:id="@+id/tabs"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/toolbar"
                        android:background="@color/primaryColor"
                        android:minHeight="?attr/actionBarSize"/>

                </RelativeLayout>

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



            <android.support.v4.view.ViewPager
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="56dp"
                android:layout_marginEnd="64dp"
                android:layout_marginStart="64dp"
                tools:context=".MainActivity" />

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <FrameLayout
                    android:id="@+id/replaceFrameLayout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"/>

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/errorWarnings"
                    android:visibility="gone"
                    android:gravity="center"
                    android:layout_gravity="center">

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/errorIcon"
                        android:tint="@color/fab_material_red_500"
                        android:layout_centerVertical="true"/>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/errorDescription"
                        android:layout_below="@+id/errorIcon"/>

                </RelativeLayout>

                <com.tim.koers.wallpapers.UI.FilterButton
                    android:id="@+id/filterButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right|bottom"
                    android:layout_margin="16dp"
                    android:clickable="true"
                    android:src="@drawable/ic_menu_filter"
                    android:elevation="6dp"
                    android:tint="@color/fab_material_white"
                    android:visibility="gone"/>

            </FrameLayout>

            <android.support.design.widget.NavigationView
                android:id="@+id/navigation_view"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="start"
                app:menu="@menu/drawer"/>

        </android.support.v4.widget.DrawerLayout>

    </FrameLayout>

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

我找到了一篇实现此布局的文章

您可以将其应用于平板电脑布局

它由一个扩展高度工具栏(蓝色)和一个带有标题和常规菜单的
CardView
组成

基本结构:

<FrameLayout>
<!-- Extended Toolbar holding Drawer icon -->
    <android.support.v7.widget.Toolbar />
    <android.support.v7.widget.CardView>
        <LinearLayout>
            <!-- Card Toolbar -->
            <android.support.v7.widget.Toolbar />
            <!-- Divider -->
            <View />
        </LinearLayout>
    </android.support.v7.widget.CardView>
</FrameLayout>



  • 经过数小时的尝试,我得出了以下代码:

    对于您的手机布局,请将其用作基础:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout
        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/coordinatorLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:theme="@style/Theme.AppCompat.NoActionBar">
    
        <android.support.v4.widget.DrawerLayout
            android:id="@+id/drawerLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
            <android.support.design.widget.AppBarLayout
                android:id="@+id/appBarLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:animateLayoutChanges="true"
                android:orientation="vertical">
    
                <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:background="@color/primaryColor"
                    android:minHeight="?attr/actionBarSize"
                    style="@style/tabsWidgetIndicatorColors"
                    android:theme="@style/Theme.AppCompat.NoActionBar">
    
                </android.support.v7.widget.Toolbar>
    
                <android.support.design.widget.TabLayout
                    android:id="@+id/tabs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/toolbar"
                    android:background="@color/primaryColor"
                    android:minHeight="?attr/actionBarSize"/>
    
            </android.support.design.widget.AppBarLayout>
    
            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
                <!-- Put your content here -->                
    
    
            </FrameLayout>
    
            </RelativeLayout>
    
            <android.support.design.widget.NavigationView
                android:id="@+id/navigation_view"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="start"
                android:fitsSystemWindows="true"
    
                app:menu="@menu/drawer"/>
    
        </android.support.v4.widget.DrawerLayout>
    
    </android.support.design.widget.CoordinatorLayout>
    
    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout
        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/coordinatorLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:theme="@style/Theme.AppCompat"
        >
    
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <android.support.v4.widget.DrawerLayout
                android:id="@+id/drawerLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                android:fitsSystemWindows="true">
    
                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
    
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="128dp"
                        android:orientation="vertical">
    
                        <android.support.v7.widget.Toolbar
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:background="@color/primaryColor"
                            android:minHeight="?attr/actionBarSize"
                            android:id="@+id/actionToolbar"
                            />
    
                        <View
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:background="@color/primaryColor"
                            android:minHeight="?attr/actionBarSize"
                            />
    
                    </LinearLayout>
    
                    <android.support.design.widget.AppBarLayout
                        android:id="@+id/appBarLayout"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:animateLayoutChanges="true"
                        android:orientation="vertical"
                        android:layout_marginEnd="64dp"
                        android:layout_marginStart="64dp"
                        android:layout_marginTop="56dp">
    
                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:orientation="vertical">
    
                            <!-- This is the secondary toolbar, 72dp also according to specs -->
                            <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
                                android:id="@+id/toolbar"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_alignParentTop="true"
                                android:background="@color/colorSecondary"
                                android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
                                android:minHeight="72dp"
                                style="@style/tabsWidgetIndicatorColors"
                                >
                                <!--android:background="?attr/colorPrimary"-->
    
                            </android.support.v7.widget.Toolbar>
    
                            <android.support.design.widget.TabLayout
                                android:id="@+id/tabs"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_below="@+id/toolbar"
                                android:background="@color/colorSecondary"
    
                                android:minHeight="?attr/actionBarSize"/>
    
                            <!--android:background="?attr/colorPrimary"-->
    
    
                        </RelativeLayout>
    
                    </android.support.design.widget.AppBarLayout>
    
    
                    <FrameLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">
    
                        <!-- your content here-->
    
    
    
                    </FrameLayout>
    
                </RelativeLayout>
    
                <android.support.design.widget.NavigationView
                    android:id="@+id/navigation_view"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_gravity="start"
                    android:fitsSystemWindows="true"
                    app:menu="@menu/drawer"/>
    
            </android.support.v4.widget.DrawerLayout>
    
        </FrameLayout>
    
    </android.support.design.widget.CoordinatorLayout>
    

    你能解释一下你到底在挣扎什么吗?你现在的问题是什么facing@XaverKapeller“我想我可能已经自己修好了。所以没问题了?”埃尔茨不再这样了。很抱歉,我将在完成编码后发布代码。本文的代码不完整。我会在今天晚些时候发送我的答案
    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout
        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/coordinatorLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:theme="@style/Theme.AppCompat"
        >
    
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <android.support.v4.widget.DrawerLayout
                android:id="@+id/drawerLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                android:fitsSystemWindows="true">
    
                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
    
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="128dp"
                        android:orientation="vertical">
    
                        <android.support.v7.widget.Toolbar
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:background="@color/primaryColor"
                            android:minHeight="?attr/actionBarSize"
                            android:id="@+id/actionToolbar"
                            />
    
                        <View
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:background="@color/primaryColor"
                            android:minHeight="?attr/actionBarSize"
                            />
    
                    </LinearLayout>
    
                    <android.support.design.widget.AppBarLayout
                        android:id="@+id/appBarLayout"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:animateLayoutChanges="true"
                        android:orientation="vertical"
                        android:layout_marginEnd="64dp"
                        android:layout_marginStart="64dp"
                        android:layout_marginTop="56dp">
    
                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:orientation="vertical">
    
                            <!-- This is the secondary toolbar, 72dp also according to specs -->
                            <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
                                android:id="@+id/toolbar"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_alignParentTop="true"
                                android:background="@color/colorSecondary"
                                android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
                                android:minHeight="72dp"
                                style="@style/tabsWidgetIndicatorColors"
                                >
                                <!--android:background="?attr/colorPrimary"-->
    
                            </android.support.v7.widget.Toolbar>
    
                            <android.support.design.widget.TabLayout
                                android:id="@+id/tabs"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_below="@+id/toolbar"
                                android:background="@color/colorSecondary"
    
                                android:minHeight="?attr/actionBarSize"/>
    
                            <!--android:background="?attr/colorPrimary"-->
    
    
                        </RelativeLayout>
    
                    </android.support.design.widget.AppBarLayout>
    
    
                    <FrameLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">
    
                        <!-- your content here-->
    
    
    
                    </FrameLayout>
    
                </RelativeLayout>
    
                <android.support.design.widget.NavigationView
                    android:id="@+id/navigation_view"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_gravity="start"
                    android:fitsSystemWindows="true"
                    app:menu="@menu/drawer"/>
    
            </android.support.v4.widget.DrawerLayout>
    
        </FrameLayout>
    
    </android.support.design.widget.CoordinatorLayout>
    
    public class MainActivity extends AppCompatActivity implements Toolbar.OnMenuItemClickListener{
    
    
    private boolean isInflateMenuEnabled = true;
    
    private Toolbar mToolbar;
    
    @Override
        protected void onCreate(Bundle savedInstanceState) {
    
    setContentView(R.layout.activity_main);
    
    
    //Check if it is a tablet or phone
    
    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    
            Toolbar actionToolbar = (Toolbar)findViewById(R.id.actionToolbar);
    
            if(actionToolbar != null){
                isInflateMenuEnabled= false;
                setSupportActionBar(actionToolbar);
    
                actionToolbar.setTitle("");
                mToolbar.inflateMenu(R.menu.menu_main);
                mToolbar.setOnMenuItemClickListener(this);
    
            }else{
    
                mToolbar.inflateMenu(R.menu.menu_main); // Inflate the menu because there will be no menu inflated automatically anymore.
                mToolbar.setOnMenuItemClickListener(this);
                setSupportActionBar(mToolbar);
           }
    
    mToolbar.setTitle(getString(R.string.title));
    
    
    }
    
    @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_main, menu);
    
    
    return isInflateMenuEnabled;
    
    }
    
    @Override
        public boolean onOptionsItemSelected(final 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.
    
    // This will be working with your menu clicks
    
    
    }
    
    
    }