移动屏幕底部的标签

时间:2018-07-11 13:21:59

标签: android xml android-tablayout

嗨,我必须在屏幕底部的Android上移动一个TabLayout,在此论坛上,我尝试了一些解决方案,方法是应用TabLayout中存在的Fragments不再可见!如何向下移动TabLayout并使片段可见?

Java代码:

super.onCreate(savedInstanceState); setContentView(R.layout.activity_tab); 
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); mViewPager = findViewById(R.id.container); 

mViewPager.setAdapter(mSectionsPagerAdapter); 

TabLayout tabLayout = findViewById(R.id.appbar); 

mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));

XML代码:

<?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/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".TabActivity">

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


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

            <android.support.design.widget.TabItem
                android:id="@+id/TabTime"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:icon="@drawable/clock"
                android:text="MarcaTempo" />

           <android.support.design.widget.TabItem
                android:id="@+id/TabHome"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:icon="@drawable/home"
                android:text="Cantieri" />


        </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.design.widget.CoordinatorLayout>

1 个答案:

答案 0 :(得分:1)

请尝试以下操作:

步骤1。在您的layout.xml上

<?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/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".TabActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        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.support.design.widget.AppBarLayout>

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

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

步骤2。在您的content.xml上

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/nav_bottom"/>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/nav_bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:menu="@menu/my_navigation_items" />

</RelativeLayout>

第3步。在您的res / menu / my_navigation_items.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/NavTime"
        android:title="MarcaTempo"
        android:icon="@drawable/clock"" />
    <item android:id="@+id/NavHome"
        android:title="Cantieri"
        android:icon="@drawable/home" />
</menu>