首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TabLayout中的意外边距

问TabLayout中的意外边距
EN

Stack Overflow用户
提问于 2018-04-23 04:07:32
回答 1查看 288关注 0票数 0

我将tabGravity的TabLayout设置为fill

代码语言:javascript
复制
<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabBackground="@drawable/tab_background"
    app:tabGravity="fill"
    app:tabIndicatorHeight="0dp"
    app:tabMaxWidth="0dp"
    app:tabMode="fixed" />

并希望在选项卡之间添加自定义分隔符。我试过这个方法

代码语言:javascript
复制
View root = tabLayout.getChildAt(0);
LinearLayout linearLayout = (LinearLayout) root;
linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
GradientDrawable drawable = new GradientDrawable();
drawable.setColor(Color.WHITE);
drawable.setSize(5, 0);
linearLayout.setDividerDrawable(drawable);

它工作得很好,除了第一个选项卡的开头。我使用的是SHOW_DIVIDER_MIDDLE,但第一个选项卡左边有空白处:

​

​

如果我删除分隔符,边距就消失了。如何在保留分隔符的同时去掉空白处?

下面是完整的测试项目:https://github.com/PiN73/TestDivider

EN

回答 1

Stack Overflow用户

发布于 2018-04-23 04:30:45

您可以为每个选项卡膨胀不同的布局。

例如:

代码语言:javascript
复制
private void setupTabs() {
    // Left tab inflate
    {
        LinearLayout tab = (LinearLayout) LayoutInflater.from(mContext)
                .inflate(R.layout.tab_left, null);
        TextView tvTabTitle = tab.findViewById(R.id.tvTabTitle);
        tvTabTitle.setText(getResources().getString(R.string.tab_left));
        tabLayout.getTabAt(0)
                .setCustomView(tab);
    }
    // Middle tab inflate
    {
        LinearLayout tab = (LinearLayout) LayoutInflater.from(mContext)
                .inflate(R.layout.tab_middle, null);
        TextView tvTabTitle = tab.findViewById(R.id.tvTabTitle);
        tvTabTitle.setText(getResources().getString(R.string.tab_middle));
        tabLayout.getTabAt(1)
                .setCustomView(tab);
    }

    // Right tab inflate
    {
        LinearLayout tab = (LinearLayout) LayoutInflater.from(mContext)
                .inflate(R.layout.tab_right, null);
        TextView tvTabTitle = tab.findViewById(R.id.tvTabTitle);
        tvTabTitle.setText(getResources().getString(R.string.tab_right));
        tabLayout.getTabAt(2)
                .setCustomView(tab);
    }

}

根据上面的示例,您有3种不同的布局,R.layout.tab_left,R.layout.tab_middle和R.layout.tab_right

您可以将这些布局安排为:

  • R.layout.tab_left :文本+分割器
  • R.layout.tab_middle : TextView
  • R.layout.tab_right :分割器+ Textview

你也可以检查这个问题,它的答案是:How to set the divider between Tabs in TabLayout of design support library?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49970254

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档