如何在屏幕底部设置android TabLayout?
我的问题是如何将新的android材质设计表格设置在屏幕底部,有点像Instagram的底部工具栏 如果您从未见过Instagram的用户界面,这里有一个屏幕截图: 。如果有更好的方法,请随意在这里发布(如果可能的话,请附上代码示例),我将不胜感激 这是我的代码:activity_main.xml如何在屏幕底部设置android TabLayout?,android,android-layout,android-tablayout,Android,Android Layout,Android Tablayout,我的问题是如何将新的android材质设计表格设置在屏幕底部,有点像Instagram的底部工具栏 如果您从未见过Instagram的用户界面,这里有一个屏幕截图: 。如果有更好的方法,请随意在这里发布(如果可能的话,请附上代码示例),我将不胜感激 这是我的代码:activity_main.xml 我已经尝试了Stack Overflow社区建议的许多方法和解决方法,但似乎没有一种能与android中标签的新实现一起工作。我知道这个UI设计没有遵循android的设计准则,所以请不要对此
我已经尝试了Stack Overflow社区建议的许多方法和解决方法,但似乎没有一种能与android中标签的新实现一起工作。我知道这个UI设计没有遵循android的设计准则,所以请不要对此发表评论。这个UI设计对我的应用程序的用户体验至关重要,我希望能得到答案。谢谢大家! 我相信我有最好的简单解决方法。使用线性布局,并将viewpager的高度设置为0dp,布局为\u weight=“1”。确保LinearLayout的方向为垂直,并且viewpager位于TableLayout之前。以下是矿山的外观:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<include layout="@layout/toolbar" android:id="@+id/main_toolbar"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/white"/>
<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/blue"
/>
</LinearLayout>
用以下代码替换您的TabLayout代码
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:elevation="2dp" />
我在Android Studio 2.2上遇到了同样的问题。这就是我所做的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical"
android:layout_height="match_parent">
<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/ThemeOverlay.AppCompat.Light"
app:subtitleTextColor="@color/color_white"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:title="@string/call_log"
app:titleTextColor="@color/color_white"
/>
<RelativeLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/tabLayout" />
<android.support.design.widget.TabLayout
android:layout_alignParentBottom="true"
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"/>
</RelativeLayout>
</LinearLayout>
最好将AppBarLayout和TableLayout分开,就像下面的代码一样。通过这种方式,您可以单独修改选项卡栏和查看寻呼机属性
<android.support.design.widget.CoordinatorLayout 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.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
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.Light" />
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_above="@+id/tabs"/>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
将此设置添加到
android.support.design.widget.TabLayout
页面顶部线性布局设置并设置
android:gravity=“bottom”
。就这样。代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px"
android:gravity="bottom"> //Thats it.
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:id="@+id/tabLayout1">
<android.support.design.widget.TabItem
android:icon="@drawable/home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tabItem1" />
<android.support.design.widget.TabItem
android:icon="@drawable/mypage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tabItem2" />
<android.support.design.widget.TabItem
android:icon="@drawable/friends"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tabItem3" />
<android.support.design.widget.TabItem
android:icon="@drawable/messages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tabItem4" />
<android.support.design.widget.TabItem
android:icon="@drawable/settings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tabItem5" />
</android.support.design.widget.TabLayout>
</LinearLayout>
//就这样。
我建议使用androidx库,因为android支持库可能很快就会被弃用
我们实际上可以将选项卡布局添加到viewpager。下面是我在项目中使用的代码片段
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00a294"
android:layout_gravity="bottom" />
</androidx.viewpager.widget.ViewPager>
layout_gravity=“bottom”是将选项卡布局置于底部的语法此处的完整解决方案:请检查我的解决方案。如果我已经解决了你们的问题,一定要把它作为一个答案。非常感谢你们。你们俩都救了我的命。你们不知道我是多么感激你们的帮助和我得到的宝贵的回答。致以最良好的祝愿@用户562非常欢迎您。快乐编码@用户562如果可能的话,尽量避免底部的标签。Instagram从第一天就开始使用类似iOS的标签。Android和Material design不建议这样做。如何在xamarin中实现它?很好,它适合我。我可以把更多的图标,然后底部导航棒伟大的答案,谢谢!我还使用了tablayout上的:app:tabIndicatorGravity=“top”将选项卡指示器移到了顶部。我认为与更复杂的答案相比,这太好了,不可能是真的。
android:layout_alignParentBottom="true"
android.support.design.widget.TabLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px"
android:gravity="bottom"> //Thats it.
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:id="@+id/tabLayout1">
<android.support.design.widget.TabItem
android:icon="@drawable/home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tabItem1" />
<android.support.design.widget.TabItem
android:icon="@drawable/mypage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tabItem2" />
<android.support.design.widget.TabItem
android:icon="@drawable/friends"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tabItem3" />
<android.support.design.widget.TabItem
android:icon="@drawable/messages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tabItem4" />
<android.support.design.widget.TabItem
android:icon="@drawable/settings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tabItem5" />
</android.support.design.widget.TabLayout>
</LinearLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00a294"
android:layout_gravity="bottom" />
</androidx.viewpager.widget.ViewPager>