2016-06-21 14 views
0

我有TabLayoutViewPager如何在Android中自定义视图的空间字符串? (TabLayout)

我想总是空间从白色圆圈文本(字符串CONVER ......)所以它会为任何决议

而当你收到一个新的消息,该选项卡与更新工作新的消息量:(查看该图像)

CLICK HERE - IMAGE

if(total_count == 0){ 
    this.tabLayout.getTabAt(1).setText(getString(R.string.str_chat)); 
} else if(total_count > 0) { 
    // if you have more than 0 messages then, reduce to Conver... 
    this.tabLayout.getTabAt(1).setText(getString(R.string.str_chat_reduced)); 
} 

的strings.xml

<string name="str_chat">CONVERSAS</string> 
<string name="str_chat_reduced">CONVER...</string> 

tab.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:showIn="@layout/app_bar_home" 
    tools:context="com.yvone.homer.HomeActivity" 
    android:orientation="vertical"> 

    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <android.support.design.widget.TabLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/tabLayout" 
      android:layout_alignParentTop="true" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" 
      style="@style/MyCustomTabLayout" /> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:weightSum="3"> 

      <LinearLayout 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:gravity="right|center_vertical" 
       android:paddingRight="5dp"> 

       <com.yvone.homer.customview.CustomFontView 
        android:layout_width="25dp" 
        android:layout_height="25dp" 
        android:textAppearance="?android:attr/textAppearance" 
        android:text="" 
        android:id="@+id/txtMatchBadge" 
        android:background="@drawable/bg_white_badge" 
        android:gravity="center" 
        android:textColor="@color/red_dark" 
        android:visibility="invisible" /> 

      </LinearLayout> 

      <LinearLayout 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:gravity="right|center_vertical" 
       android:paddingRight="5dp"> 

       <com.yvone.homer.customview.CustomFontView 
        android:layout_width="25dp" 
        android:layout_height="25dp" 
        android:textAppearance="?android:attr/textAppearance" 
        android:text="" 
        android:id="@+id/txtChatBadge" 
        android:background="@drawable/bg_white_badge" 
        android:gravity="center" 
        android:textColor="@color/red_dark" 
        android:visibility="invisible" /> 

      </LinearLayout> 

      <LinearLayout 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:gravity="right|center_vertical" 
       android:paddingRight="5dp"/> 
     </LinearLayout> 
    </FrameLayout> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/pager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

    </android.support.v4.view.ViewPager> 
</LinearLayout> 

回答

0

你会提供您的自定义视图选项卡,通过tabForCustom.setCustomView(R.layout.your_custom_view)TextViewImageView的最佳解决方案。

当使用viewPager你也可以在overrided方法getPageTitle使用SpannableStringPageAdapter class

 @Override 
     public CharSequence getPageTitle(int position) { 

      String tabName = "Section " + position; 
      Drawable image = getApplicationContext().getResources().getDrawable(android.R.drawable.ic_menu_info_details); 
      image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight()); 
      SpannableString sb = new SpannableString(tabName + "x"); 
      ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM); 
      sb.setSpan(imageSpan, tabName.length() - 1, sb.length()-1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
      return sb; 
} 

如果u需要只是一个图标 - 使用setIcon方法:

tabLayout.getTabAt(i).setIcon(android.R.drawable.ic_menu_info_details); 

而且不使用另一个字符串资源减少长度,为TextView提供elipsize属性。更多文档here

相关问题