【问题标题】:Android TabPage Indicator content wrapping issueAndroid TabPage Indicator 内容包装问题
【发布时间】:2012-06-28 10:13:34
【问题描述】:

我遇到了 ViewPager 的另一个问题,我目前无法以我目前的知识解决它。

我有 TabPageIndicator 和 ViewPager,在每个选项卡上,我都在显示文本。很简单的textView:

<?xml version="1.0" encoding="utf-8"?>
<view xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    class="viewpagerindicator.TabPageIndicator$TabView" >

    <TextView
        android:id="@+id/vpi_tab_text"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:gravity="center"
        android:paddingBottom="10dip"
        android:paddingLeft="8dip"
        android:paddingRight="8dip"
        android:paddingTop="10dip"
        android:textColor="@drawable/vpi_tab_text_color"
        android:typeface="serif" />

</view>

我希望选项卡中的文本始终是单行并且始终是整个文本,而不是椭圆形,不换行,不超过 1 行。用户必须能够完整阅读它...

但我做不到,我尝试了不同的选项,但仍然遇到一些问题 - 文本不止一行,但只显示第一行,或者只有部分文本显示出来。

你有过类似的经历吗?

感谢任何帮助

编辑一个:

我相信问题出在这里:

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
final boolean lockedExpanded = widthMode == MeasureSpec.EXACTLY;
setFillViewport(lockedExpanded);
    final int childCount = mTabLayout.getChildCount();
    if (childCount > 1 && (widthMode == MeasureSpec.EXACTLY || widthMode == MeasureSpec.AT_MOST)) {
        if (childCount > 2) {
            mMaxTabWidth = (int) (MeasureSpec.getSize(widthMeasureSpec) * 0.4f);
        } else {
            mMaxTabWidth = MeasureSpec.getSize(widthMeasureSpec) / 2;
        }
    } else {
        mMaxTabWidth = -1;
    }

    final int oldWidth = getMeasuredWidth();
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    final int newWidth = getMeasuredWidth();

    if (lockedExpanded && oldWidth != newWidth) {
        // Recenter the tab display if we're at a new (scrollable) size.
        setCurrentItem(mSelectedTabIndex);
    }
 }

mMaxTabWidth...

【问题讨论】:

    标签: android android-viewpager tabpage indicator viewpagerindicator


    【解决方案1】:

    我觉得问题很简单,我把这部分注释掉了:

            @Override
            public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
                super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    
                // Re-measure if we went beyond our maximum size.
    //          if (mParent.mMaxTabWidth > 0 && getMeasuredWidth() > mParent.mMaxTabWidth) {
    //              super.onMeasure(MeasureSpec.makeMeasureSpec(mParent.mMaxTabWidth, MeasureSpec.EXACTLY), heightMeasureSpec);
    //          }
            }
    

    现在它按我想要的方式工作了...我一直在查看代码的错误部分。

    【讨论】:

      【解决方案2】:

      当它不应该省略标签中的文本时,我遇到了问题。我使用@larham 的建议找到了修复here, #227。这基本上是他所做的,但最好阅读他对他所做的事情以及他为什么这样做的解释:

      在 TabPageIndicator.java 中,替换以下内容:

      mTabLayout.addView(tabView, new LinearLayout.LayoutParams(0, MATCH_PARENT, 1));
      

      与:

      mTabLayout.addView(tabView, new LinearLayout.LayoutParams(WRAP_CONTENT, MATCH_PARENT, 1));
      

      这为我解决了问题。

      【讨论】:

        【解决方案3】:

        我遇到了类似的问题,没有显示整个文本。 我解决了它减少左/右样式填充。

        【讨论】:

          【解决方案4】:

          我使用 Wrap content 属性,而不是宽度的 Layout weight=1。 在 TabPageIndicator 类的 addTab(int,CharSequence,int) 方法中,将 mTab​​Layout.addView 替换为以下内容:

          mTabLayout.addView(tabView, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));

          【讨论】:

            【解决方案5】:

            更新 ViewPageIndicator Library :) 已正式修复 :)

            【讨论】:

            • 问题是:“你遇到过类似的事情吗?感谢任何帮助”.. 我遇到了同样的问题,它已修复为从 git repo 中提取最新版本。那为什么不呢?
            猜你喜欢
            • 2018-01-11
            • 2012-08-28
            • 1970-01-01
            • 2016-03-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多