如何实现一个 TabLayout,其中的选项卡从左或右开始,但不是居中?

How can I implement a TabLayout with tabs that start from left or right, but not center?

如何实现 TabLayout,其中选项卡从左侧或右侧开始,但不正确地从中心开始。

TabLayout,有一个 属性 app:tabGravity="center",但我如何在特定位置(左侧或右侧)排列我的选项卡?

非常感谢。

这是截图,如果可能的话,更多关于我的想法的信息。

如果可能,我想将 'Inizio Sessione' 选项卡从中间向左或向右移动。

谢谢。

enter image description here

这是代码:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".fragments.StartSessionContainerFragment">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="2dp"
        android:layout_marginTop="2dp"
        app:layout_scrollFlags="scroll|enterAlways|snap">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:elevation="6dp"
            android:minHeight="?attr/actionBarSize"
            android:layout_margin="0dp"
            app:tabPaddingStart="20dp"
            app:tabPaddingEnd="10dp"
            app:tabGravity="center"
            app:tabMode="scrollable"
            android:layout_gravity="left"
            android:background="?attr/colorPrimary" />

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

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

</RelativeLayout>

下面是适合我的。

选项卡将显示在左侧。

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="2dp"
        android:layout_marginTop="2dp"
        app:layout_scrollFlags="scroll|enterAlways|snap">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="0dp"
            android:background="?attr/colorPrimary"
            android:elevation="6dp"
            android:minHeight="?attr/actionBarSize"
            app:tabGravity="fill"
            app:tabMode="scrollable"
            app:tabPaddingEnd="10dp"
            app:tabPaddingStart="20dp" />

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

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

</RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    //This is our tablayout
    private TabLayout tabLayout;

    //This is our viewPager
    private ViewPager viewPager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        tabLayout = findViewById(R.id.tabs);
        viewPager = findViewById(R.id.view_pager);

        tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
       // tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
       // tabLayout.addTab(tabLayout.newTab().setText("tab3"));

        Pager adapter = new Pager(getSupportFragmentManager(), tabLayout.getTabCount());
        viewPager.setAdapter(adapter);

    }

让我知道更多查询。 }

简单的方法是您只需执行下面的代码并将您想要的任何选项卡设置为默认选项卡即可;

viewPager.setCurrentItem(2);