【问题标题】:Android - How to left align single tab in TabLayoutAndroid - 如何在 TabLayout 中左对齐单个选项卡
【发布时间】:2021-04-16 23:50:10
【问题描述】:

我有一个TabLayoutAppBarLayoutToolBarViewPager 一起使用,以便拥有可滚动的标签。目前,我只有一个标签,但标签不是左对齐,而是居中。

<?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"
    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.TabLayout
            android:id="@+id/tablayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

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

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

【问题讨论】:

    标签: android xml android-tablayout


    【解决方案1】:

    将您的 TabLayout 属性设置为: 应用程序:tabGravity="center" app:tabMode="可滚动"

    希望对您有所帮助。

    【讨论】:

    • app:tabMode="scrollable" 对我来说就足够了。
    【解决方案2】:

    尝试使用 XML 属性集

    android:layout_gravity="left"
    

    这就是我能找到的关于这个主题的全部记录

    set layout_gravity 是选项卡式布局的值。

    更多documentation here。还有here

    【讨论】:

    • 那行不通。另外,我的 TabLayout 在 AppBarLayout 下而不是 ViewPager 下。我相信左对齐适用于整个 TabLayout,而不适用于单个标签。
    • 好的,我明白了。您是否尝试过与父母保持一致? layout_alignParentLeft?
    • 刚刚尝试过 - 没有运气。问题是我更改的任何布局参数我认为影响整个 TabLayout 而不是选项卡。有像app:tabContentStart 这样的参数,但它们也没有做任何事情。
    • 你说你只有一个标签?为什么要让它左对齐?好像没那么好看?
    • 哈哈,让我们不要进入设计辩论。当它居中时,它看起来有点奇怪。我更喜欢左对齐
    【解决方案3】:

    只需将 TabLayout 宽度更改为 wrap_content... 这将使单个选项卡左对齐

    【讨论】:

      猜你喜欢
      • 2015-11-12
      • 1970-01-01
      • 2021-12-06
      • 2015-02-19
      • 2020-06-05
      • 2016-11-11
      • 2017-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多