在中心对齐标签图标时出现问题

时间:2016-08-22 16:05:42

标签: android android-layout

我试图将滑动标签图标对齐在中心,但不明白为什么它没有发生。

enter image description here

请查看我的viewPager适配器的getPageTitle方法

适配器类

 class MyPagerAdapter extends FragmentPagerAdapter {

        int[] icons = {R.drawable.clapperboard, R.drawable.documentary, R.drawable.widescreen};
        String[] tabs;

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
            tabs = getResources().getStringArray(R.array.tabs);
        }

        @Override
        public Fragment getItem(int position) {
            MyFragment myFragment = MyFragment.getInstance(position);
            return myFragment;
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            Drawable drawable;//=getResources().getDrawable(icons[position]);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                drawable = getResources().getDrawable(icons[position], getApplicationContext().getTheme());
            } else {
                drawable = getResources().getDrawable(icons[position]);
            }
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
            ImageSpan imageSpan = new ImageSpan(drawable);
            SpannableString spannableString = new SpannableString(" ");
            spannableString.setSpan(imageSpan, 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);


            return spannableString;
        }
    }

OnCreate方法

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

        toolbar = (Toolbar) findViewById(R.id.app_bar);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        setSupportActionBar(toolbar);

        NavigationDrawerFragment navigation_drawer = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_navigate_drawer);
        navigation_drawer.setUp(R.id.fragment_navigate_drawer, mDrawerLayout, toolbar);

        mTab = (SlidingTabLayout) findViewById(R.id.tabs);
        mPager = (ViewPager) findViewById(R.id.pager);
        mPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
        mTab.setDistributeEvenly(true);
        mTab.setCustomTabView(R.layout.custom_tab_view, R.id.txtCustom);
        mTab.setViewPager(mPager);
    }

custom_tab_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8dp"
    android:orientation="vertical">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:id="@+id/icnImg"/>

    <TextView
        android:layout_width="match_parent"
        android:id="@+id/txtCustom"
        android:layout_height="wrap_content" android:layout_gravity="bottom|center"
        />
</LinearLayout>

0 个答案:

没有答案