【问题标题】:TabItem in center when i choose it当我选择它时,TabItem 在中心
【发布时间】:2020-06-14 11:37:03
【问题描述】:

我有一个可滚动的 tablayout,但是当我选择一个 tabitem 时,可滚动停止,当我选择任何一个时,我需要在中心的 TabItem。谢谢。

<com.google.android.material.tabs.TabLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabSelectedTextColor="@color/colorPrimary">

            <com.google.android.material.tabs.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Calle Mármol complejo 23" />

            <com.google.android.material.tabs.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Calle Mármol complejo 23" />
        <FrameLayout
                android:id="@+id/fragment_container"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

现在是 java 文件,只是为了转到 tabitem

 @Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

    boolean fragmentTransaction = false;
    Fragment fragment = null;

    switch (menuItem.getItemId()) {
        case R.id.nav_home:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new HomeFragment()).commit();
            titleToolbar = getResources().getString(R.string.Menu_Home);
            fragmentTransaction = true;
            getSupportActionBar().setTitle(titleToolbar);
            break;

        case R.id.nav_receipts:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new ReceiptsFragment()).commit();
            titleToolbar = getResources().getString(R.string.Menu_Receipts);
            fragmentTransaction = true;
            getSupportActionBar().setTitle(titleToolbar);
            break;

【问题讨论】:

    标签: java android tabitem


    【解决方案1】:

    XML:

    <androidx.appcompat.widget.LinearLayoutCompat
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
            <com.google.android.material.tabs.TabLayout
                android:id="@+id/tabs"
                style="@style/TabStyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:elevation="5dp"
                app:tabMode="scrollable" />
    
            <androidx.viewpager.widget.ViewPager
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
    
        </androidx.appcompat.widget.LinearLayoutCompat>
    

    样式

     <style name="TabStyle" parent="Widget.Design.TabLayout">
            <item name="android:background">@color/colorPrimary</item>
            <item name="tabTextAppearance">@style/TabTextStyle</item>
            <item name="tabIndicatorColor">@color/white</item>
            <item name="tabTextColor">@color/red_light</item>
            <item name="tabSelectedTextColor">@color/white</item>
        </style>
    
        <style name="TabTextStyle" parent="TextAppearance.AppCompat.Button">
            <item name="android:textSize">@dimen/_10sdp</item>
        </style>
    

    Java

    public class MainActivity extends AppCompatActivity {
      private SampleAdapter adapter;
      private TabLayout tabs;
    
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        ViewPager pager=(ViewPager)findViewById(R.id.pager);
    
        adapter=new SampleAdapter(this, getSupportFragmentManager());
        pager.setAdapter(adapter);
    
        tabs=(TabLayout)findViewById(R.id.tabs);
        tabs.setupWithViewPager(pager);
        tabs.setTabMode(TabLayout.MODE_SCROLLABLE);
      }
    }
    

    根据tabItem在Adapter中分配你的片段

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多