【问题标题】:Spacing between tabs TabPageIndicator选项卡之间的间距 TabPageIndicator
【发布时间】:2015-08-22 07:07:37
【问题描述】:

我正在尝试实现我的设计师为我设计的设计。

我无法将 CompoundDrawable 移近文本(设置 padding 属性可以将其移得很远,但即使是 0dp 也无法使其靠近),并且我无法通过制表符之间的间距使宽度保持不变。

这是我得到的(不要看字体、文本颜色、下划线大小,目前正在开发中):

但我在自定义 Jake's Wharton library 时遇到了麻烦

这是几乎相同的问题:Space between elements of the indicator

<style name="SkillsPageIndicator" parent="Widget.TabPageIndicator">
        <item name="android:background">@drawable/tab_underline_indicator</item>
        <item name="android:textSize">@dimen/text_size_medium</item>
        <item name="android:paddingTop">35dp</item>
        <item name="android:paddingBottom">16dp</item>
        <item name="android:drawablePadding">0dp</item>
        <item name="android:gravity">center</item>
        <item name="android:width">100dp</item>
</style>

【问题讨论】:

    标签: android xml tabs android-viewpager viewpagerindicator


    【解决方案1】:

    我编写的代码不太干净,但它对我有用。我在这里分享它以帮助任何需要它的人:

    更改 TabPageIndicator.addTab 中的代码:

    private void addTab(int index, CharSequence text, int iconResId) {
            final TabView tabView = new TabView(getContext());
            tabView.mIndex = index;
            tabView.setFocusable(true);
            tabView.setOnClickListener(mTabClickListener);
            tabView.setText(text);
    
            /**
             *  My parts (CullyCross):
             */
    
            Resources r = getResources();
            DisplayMetrics metrics = r.getDisplayMetrics();
            float screenWidth  = metrics.widthPixels;
    
            float width = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, metrics);
            int margin = (int)(screenWidth - 2 * width) / 4;
    
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int)width, MATCH_PARENT);
            params.setMargins(margin, 0, margin, 0);
    
    
            /***********************/
    
            if (iconResId != 0) {
                tabView.setCompoundDrawablesWithIntrinsicBounds(iconResId, 0, 0, 0);
            }
    
            /**
             * original
             * mTabLayout.addView(tabView, new LinearLayout.LayoutParams(0, MATCH_PARENT, 1));
             */
    
            mTabLayout.addView(tabView, params);
        }
    

    字体细一点会更好看。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-02
      • 2021-05-08
      • 2011-12-15
      • 2016-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多