猿问

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

我如何实现一个TabLayout,其中选项卡从左侧或右侧开始,但不是从中心开始。

TabLayout, 有一个属性app:tabGravity="center", 但我怎样才能安排我的标签, 在一个特定的位置, 左或右?

非常感谢。


FFIVE
浏览 260回答 3
3回答

慕容3067478

下面是为我工作。选项卡将显示在最左边。<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; xmlns:app="http://schemas.android.com/apk/res-auto"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; android:orientation="vertical">&nbsp; &nbsp; <android.support.design.widget.AppBarLayout&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginStart="2dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginTop="2dp"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_scrollFlags="scroll|enterAlways|snap">&nbsp; &nbsp; &nbsp; &nbsp; <android.support.design.widget.TabLayout&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/tabs"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_margin="0dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:background="?attr/colorPrimary"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:elevation="6dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:minHeight="?attr/actionBarSize"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app:tabGravity="fill"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app:tabMode="scrollable"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app:tabPaddingEnd="10dp"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app:tabPaddingStart="20dp" />&nbsp; &nbsp; </android.support.design.widget.AppBarLayout>&nbsp; &nbsp; <android.support.v4.view.ViewPager&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/view_pager"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; app:layout_behavior="@string/appbar_scrolling_view_behavior" /></RelativeLayout>MainActivity.javapublic class MainActivity extends AppCompatActivity {&nbsp; &nbsp; //This is our tablayout&nbsp; &nbsp; private TabLayout tabLayout;&nbsp; &nbsp; //This is our viewPager&nbsp; &nbsp; private ViewPager viewPager;&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.activity_main);&nbsp; &nbsp; &nbsp; &nbsp; tabLayout = findViewById(R.id.tabs);&nbsp; &nbsp; &nbsp; &nbsp; viewPager = findViewById(R.id.view_pager);&nbsp; &nbsp; &nbsp; &nbsp; tabLayout.addTab(tabLayout.newTab().setText("Tab1"));&nbsp; &nbsp; &nbsp; &nbsp;// tabLayout.addTab(tabLayout.newTab().setText("Tab2"));&nbsp; &nbsp; &nbsp; &nbsp;// tabLayout.addTab(tabLayout.newTab().setText("tab3"));&nbsp; &nbsp; &nbsp; &nbsp; Pager adapter = new Pager(getSupportFragmentManager(), tabLayout.getTabCount());&nbsp; &nbsp; &nbsp; &nbsp; viewPager.setAdapter(adapter);&nbsp; &nbsp; }让我知道更多查询。}

HUX布斯

这是截图,如果可能的话,可以了解更多关于我的想法的信息。如果可能的话,我想将“Inizio Sessione”选项卡从中间移动到左侧或右侧。谢谢。这是代码:   <?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>

GCT1015

简单的方法是您可以简单地执行下面的代码并将您想要的任何选项卡设置为默认选项卡;viewPager.setCurrentItem(2);
随时随地看视频慕课网APP

相关分类

Java
我要回答