【问题标题】:How to achieve this layout on tablet?如何在平板电脑上实现这种布局?
【发布时间】:2016-05-11 09:39:59
【问题描述】:

我正在尝试为平板电脑/大屏幕设备创建一个好的 UI 界面。

完美的解决方案是在 GMail 应用上运行(见下面的截图)。

据我了解,布局是这样组成的:

<?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">

        <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="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" />
        </android.support.design.widget.AppBarLayout>

        <include layout="@layout/activity_main_twopane" />

        <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"
            android:src="@drawable/ic_add" />

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

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

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

但如果我采用我的解决方案,这会给我带来 2 个问题:

  1. 我的代码中的FloatingActionButton 位于布局的底部|端;在 GMail 应用程序中,这仅位于列表布局(包含电子邮件的布局)上;
  2. 我的代码中的Toolbar 只有一个(我也有一个搜索操作),但在 GMail 应用程序上似乎有 2 个:一个作为主要搜索选项,一个第二个是选择电子邮件时可见的详细操作。

有什么建议可以让我在屏幕截图上实现布局?

【问题讨论】:

    标签: android android-layout android-tablet-layout


    【解决方案1】:

    快速回答您的问题 2 和 3:

    1. 为了将 FAB 放置在所需位置,您必须在 FloatingActionButton xml 代码上添加此标签:app:layout_anchor="@id/desired_view_id"app:layout_anchorGravity="bottom|right|end"
    2. 在底部工具栏中,您可以定义单独的工具栏并为其设置样式。

    见下方代码

       // this is top toolbar
       Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar_top);
       setSupportActionBar(toolbarTop);
    
       // this is bottom one
       Toolbar toolbarBottom = (Toolbar) findViewById(R.id.toolbar_bottom);
       toolbarBottom.setOnMenuItemClickListener(new         Toolbar.OnMenuItemClickListener() {
          @Override
          public boolean onMenuItemClick(MenuItem item) {
              switch(item.getItemId()){
                  case R.id.action_settings:
                      // TODO
                      break;
                  // TODO: Other cases
              }
              return true;
          }
       });
       // Inflate a menu to be displayed in the toolbar
       toolbarBottom.inflateMenu(R.menu.menu_main);
    

    【讨论】:

    • 太好了,谢谢!我不知道 layout_anchor 和他的重力设置;顺便说一句,它不是我想要的底部工具栏,但您确认我可以使用超过 1 个工具栏并使用它。关于第一个问题的任何线索?
    • 我已经编辑了我原来的问题:你的回答是一个很好的解决方案,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2018-12-16
    • 1970-01-01
    • 1970-01-01
    • 2012-09-23
    • 2011-05-10
    • 2014-06-18
    • 2011-10-31
    相关资源
    最近更新 更多