簡體   English   中英

選項卡不填充選項卡布局

[英]The tabs dont fill the tablayout

選項卡不會像這樣填充選項卡布局

當每個tab == max width/2時,如何使它們充滿標簽的布局

主要活動java:

ViewPager mviewPager = (ViewPager)findViewById(R.id.pager);
    mviewPager.setAdapter(new viewpager(getSupportFragmentManager(),MainActivity.this));

    tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
    tabLayout.setupWithViewPager(mviewPager);

主要活動xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/Colorprimary"
    android:elevation="4dp"
    android:id="@+id/my_tool_bar"
   />

<android.support.design.widget.TabLayout
    android:id="@+id/sliding_tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabMode="scrollable"
    />

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

<android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/pager"
    android:layout_weight="1"
    />

片段尋呼機apdater java:

    package com.example.smite.floater;


import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.widget.Switch;

public class viewpager extends FragmentPagerAdapter {

    final int fragments_counter=2;
    private String titles[] = new String[]{"main","creator"};
    private Context context;

    public viewpager(FragmentManager fm , Context context){
        super(fm);
        this.context = context;
    }

    @Override
    public int getCount() {
        return fragments_counter;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position){

            case 0:
                return new main();
            case 1:
                default:
                return new creator();
        }
    }

    @Override
    public CharSequence getPageTitle(int position) {
        // Generate title based on item position
        return titles[position];
    }

}

我的應用程序主題父級是parent="Theme.AppCompat.Light.NoActionBar"

感謝您的幫助(:

如果將行"app:tabMode="scrollable"放進去,那么布局布局的寬度將不會填滿整個屏幕。

    app:tabMode="fixed"

在Java中

    if(tabLayout.getTabCount() > MAX_NUMBER) {
       tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
    }

MAX_NUMBER是一些數字,具體取決於您的要求。 我用過3,您可以使用3,4或任何適合您的要求。 這將確保如果只有幾個選項卡,它們將占據整個寬度,如果沒有,則可滾動。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM