android - 选项卡之间的间距 TabPageIndicator

标签 android xml tabs android-viewpager viewpagerindicator

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

我无法将 compoundDrawable 移近文本(设置 padding 属性可以将其移远,但即使是 0dp 也无法使其靠近)并且我无法使标签之间的间距保持恒定宽度。

Tabs design

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

enter image description here

但我在定制 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>

最佳答案

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

done image

更改 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);
    }

使用更细的字体看起来会更好。

关于android - 选项卡之间的间距 TabPageIndicator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32148821/

相关文章:

android - ViewPager Fragment 不同步更新?

android - 每个Android屏幕的尺寸是多少

android - XML 在一行中为所有边声明填充或边距

c# - 数据驱动测试消除空白

不同选项卡中的 Android WebView

java - 错误 incompatible types : android. app.FragmentManager cannot be converted to android.support.v4.app.FragmentManager

Angular Material 选项卡内的 Angular react 形式给出错误

android - 如何使用 appium 获取已经运行的应用程序的上下文?

javascript - 使用 jQuery 从 XML 加载 Highcharts 系列

android - 反向转发 - 在某种程度上