TabLayout ViewPager图标在Android的角落显示

时间:2016-11-23 07:34:27

标签: android android-viewpager android-tablayout

我已经使用三个标签实现了ViewPager,每个标签只有图标,它将以左对齐方式显示。我期望的输出是标签将等于宽度空间。这是我必要的代码和屏幕截图。请指导我解决问题,提前致谢。

layout.xml

<android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            app:tabMode="scrollable"
            app:tabIndicatorColor="@color/colorTrade_2"
            app:tabPaddingBottom="-10dp"
            app:tabPaddingEnd="-10dp"
            app:tabPaddingStart="-10dp"
            app:tabPaddingTop="-10dp"
            app:tabGravity="fill"/>

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

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

JustTabActivity.Class

import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;

public class JustTabActivity extends AppCompatActivity {

    private TabLayout tabLayout;
    private ViewPager parent_viewpager;
    ViewPagerAdapter pageAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.aaa_sms_layout);

        parent_viewpager = (ViewPager) findViewById(R.id.parent_viewpager);
        setupViewPager(parent_viewpager);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(parent_viewpager);
        tabLayout.setFillViewport(true);
        parent_viewpager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(onTabSelectedListener(parent_viewpager));
        setupTabIcons();
    }


    private void setupTabIcons() {
        int[] tabIcons = {R.drawable.icon1,R.drawable.icon5,R.drawable.ic_settings };

        tabLayout.getTabAt(0).setIcon(tabIcons[0]);
        tabLayout.getTabAt(1).setIcon(tabIcons[1]);
        tabLayout.getTabAt(2).setIcon(tabIcons[2]);

        tabLayout.getTabAt(0).getIcon().setColorFilter(Color.parseColor("#018FFD"), PorterDuff.Mode.SRC_IN);
        tabLayout.getTabAt(1).getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
        tabLayout.getTabAt(2).getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);

    }

    private void setupViewPager(ViewPager viewPager) {
        pageAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        pageAdapter.addFrag(new fragment_home_new());
        pageAdapter.addFrag(new fragment_userprofile());
        pageAdapter.addFrag(new fragment_settings());
        parent_viewpager.setAdapter(pageAdapter);
    }


    class ViewPagerAdapter extends FragmentStatePagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            System.out.println(""+mFragmentList.size());
            return mFragmentList.size();
        }

        public void addFrag(Fragment fragment) {
            mFragmentList.add(fragment);

        }


        @Override
        public CharSequence getPageTitle(int position) {
            return null;
        }
    }

    private TabLayout.OnTabSelectedListener onTabSelectedListener(final ViewPager mPager) {
        return new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mPager.setCurrentItem(tab.getPosition());
                tab.getIcon().setColorFilter(Color.parseColor("#018FFD"), PorterDuff.Mode.SRC_IN);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) { }
        };
    }
}

Screenshot of my output, Please Help to set Tabs Equally

2 个答案:

答案 0 :(得分:2)

select item_code ,item_name ,(select sum(qty) from stock_ledger where item_code = im.item_code, and location_code in ('D2', 'D4', 'D5', 'D11') and documentdate <= to_date(:TO_DATE, 'MM/DD/YYYY') and documentdate >= to_date(:FROM_DATE, 'MM/DD/YYYY')) as something1 ,ir.srg(im.item_code, 'Company1', 'ALL', 'C', '01/01/2009', '12/31/2010') , -- Function Called with other Conditions from item_master im 替换为layout.xml中的app:tabMode="scrollable"

答案 1 :(得分:1)

tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setTabMode(TabLayout.MODE_FIXED);