【问题标题】:Inflated tab items in Tab Layout does not match parent in android选项卡布局中的膨胀选项卡项与 android 中的父项不匹配
【发布时间】:2016-12-02 06:16:09
【问题描述】:

我已经完成了这些,但似乎没有用

合作

android.support.design.widget.TabLayout

需要为每个子选项卡设置自定义视图,以匹配父子大小

  • 务实地给出一个尺寸

        View v = LayoutInflater.from(this).inflate(R.layout.layout_two, null);
        v.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        tabLayout.getTabAt(0).setCustomView(v);
    

这可以作为匹配父级,因为没有背景但是一旦添加膨胀视图问题就会出现

 tabLayout.getTabAt(1).setText("About").setIcon(R.drawable.drop_complete);
  • 虽然关于 tabIndicator 也是如此 将其高度设置为 app:tabIndicatorHeight="0dp" 或添加 android:tabStripEnabled="false" 无济于事。

这是一张带有边界的图片,清楚地显示了像padding这样的小间隙(蓝色条是我的带有两个标签的TabLayout)

左边是充气的 右边是正常的.setIcon

任何帮助使一个匹配的父级没有间隙?

这是我的代码

XML

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:elevation="30dp">


        //------- INSIDE THIS I HAVE A TAB
        <android.support.v7.widget.Toolbar
            android:background="@drawable/capture"
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:minHeight="?attr/actionBarSize"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:elevation="0dp"
            app:layout_scrollFlags="scroll|enterAlways"
            />


        //------ HERE comes THE TAB  That I DEAL 
        <android.support.design.widget.TabLayout
            android:tabStripEnabled="false"
            app:tabIndicatorHeight="0dp"
            android:id="@+id/tab_layout"
            android:scrollbars="none"
            android:layout_below="@+id/toolbar"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            app:tabTextColor="#d3d3d3"
            android:minHeight="?attr/actionBarSize"
            android:orientation="vertical" >

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


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

    <android.support.v4.view.ViewPager
        android:background="#467"
        android:id="@+id/viewpager"
        android:layout_below="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        />

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

我的班级

public class LayoutExampleActivity extends AppCompatActivity {
    private TabLayout tabLayout;


    private ViewPager viewPager;

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

        tabLayout = (TabLayout) findViewById(R.id.tab_layout);


        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPagerAdaptor(viewPager);

        tabLayout.setupWithViewPager(viewPager);
        setTabs();


    }

    private void setupViewPagerAdaptor(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new FragmentOne(), "First");
        adapter.addFragment(new GalleryFragment(), "Second");
//        adapter.addFragment(new FragmentOne(), "Third");
        viewPager.setAdapter(adapter);
    }

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

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

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

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }

    public void setTabs() {

        tabLayout.getTabAt(0).setCustomView(R.layout.layout_one);

        tabLayout.getTabAt(1).setText("About").setIcon(R.drawable.drop_complete);

    }

}

【问题讨论】:

    标签: android tabs material-design tabitem


    【解决方案1】:

    设置tabPaddingStarttabPaddingEnd 属性。 像这样:

        <android.support.design.widget.TabLayout
                ...
                app:tabPaddingStart="0dp"
                app:tabPaddingEnd="0dp" >
    

    此问题可能重复。
    见:Cannot remove Padding from Tabs when using Custom views with Tab Layout

    【讨论】:

    • 我在问题中明确提到 0 dp 不起作用
    • app:tabIndicatorHeight="0dp" 在您的问题中被提及。我建议你试试tabPaddingStarttabPaddingEnd。它适用于我的 XML 环境。
    • 终于找到正确答案了!它正在工作!看起来在 Tablet android 的 tablayout 上的工作方式不同。如果您没有定义选项卡填充,即使您定义了 tabMaxWidth="0dp" 也将不起作用。
    猜你喜欢
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    相关资源
    最近更新 更多