【问题标题】:How to combine the Toolbar and a TabLayout in landscape orientation?如何在横向组合工具栏和 TabLayout?
【发布时间】:2017-12-01 10:01:21
【问题描述】:

如何在 youtube 应用程序中显示的横向模式下组合工具栏和 Tablayout?我似乎找不到明确的答案。

我目前的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        app:tabMode="fixed"
        app:tabGravity="fill"/>

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

    </LinearLayout>

【问题讨论】:

  • 这是来自标签的屏幕截图,因为我在我的设备中看不到这种行为
  • 提示:工具栏是ViewGroup。您可以将TabLayout 放入其中。在横向模式下的平板电脑布局中。
  • 这确实是 Lollypop 设备上最新版本应用程序的屏幕截图。它处于横向模式。
  • @R.Zagórski 这可以在 XML 中完成吗?
  • 当然,你试过了吗?看how to add content 到Toolbar。

标签: android android-view android-tabs android-tablayout


【解决方案1】:

如上所述,Toolbar 是一个 ViewGroup,因此 TabLayout 可以成为它的子级。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
        android:title="Viewer">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:layout_gravity="center_horizontal"
        app:tabMode="fixed"
        app:tabGravity="center"/>
    </android.support.v7.widget.Toolbar>

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

</LinearLayout>

【讨论】:

    【解决方案2】:

    您可以将带有组件的布局添加到 ActionBar(经典布局,而不是 android.support.v7.widget.Toolbar)

    首先创建一个菜单资源 menu\top_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/topBarTabLayout"
    android:title=""
    app:actionLayout="@layout/top_bar_tab_layout"
    app:showAsAction="always" />
    
              <!-- Other menu items here -->
    
    </menu>
    

    然后,创建一个名为 layout/top_bar_tab_layout.xml 的布局,其中只包含一个 TabLayout 组件(您也可以添加其他组件!):

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.TabLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tab_layout_menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="0dp"
        android:paddingTop="0dp" />
    

    要访问此 TabLayout 组件,请在您的活动中创建一个引用 onCreateOptionsMenu:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.top_menu, menu);
    
    topBarTabLayoutMenuItem = menu.findItem(R.id.topBarTabLayout);
    topBarTabLayout = (TabLayout) topBarTabLayoutMenuItem.getActionView();
    
    //ADD #1 TAB WITH A ICON RESOURCE
    topBarTabLayout.addTab(topBarTabLayout.newTab().setIcon(R.drawable.file));
    
    //ADD #2 TAB WITH A ICON RESOURCE
    topBarTabLayout.addTab(topBarTabLayout.newTab().setIcon(R.drawable.folder));
    
    //This may be necessary to force tablayout to fill all space.
    topBarTabLayout.setTabGravity(topBarTabLayout.GRAVITY_FILL);
    
    
    //Add listener to do stuff!
    topBarTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    //do whatever you want here, like selecting a viewpager item or open an intent
                }
    
                @Override
                public void onTabUnselected(TabLayout.Tab tab) {
            //do whatever you want here
                }
    
                @Override
                public void onTabReselected(TabLayout.Tab tab) {
            //do whatever you want here
                }
            });
    
            return true;
        }
    }
    

    这应该足以将 tabLayout 添加到您的 actionBar。它将与微调器或 textViews 等其他组件一起使用。

    此外,如果您打算将此选项卡布局设置为仅在方向为横向时可见,则必须执行以下操作:

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    
        if (newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE) {
                    topBarTabLayoutMenuItem.setVisible(true);
            } else {
            topBarTabLayoutMenuItem.setVisible(false);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多