2017-08-10 93 views
0

我用tablayout製作了一個viewpager,我用自定義視圖來顯示服務器上的圖標,一切正常,但標籤圖標沒有完全顯示.i上傳了一張圖片。如何解決這個問題。我試圖與圖像高度寬度匹配父母或靜態大小像50dp等,但沒有任何工作。如何使tablayout圖標變大/適合每個標籤空間?

我tablayout:爲圖標

<android.support.v4.view.ViewPager 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/smileyShowViewPagerId" 
     android:background="#ececec" 
     > 
     <android.support.design.widget.TabLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/tabLayoutId" 
      android:background="#99a4b1" 
      app:tabTextColor="#000000" 
      app:tabMode="fixed" 
      app:tabIndicatorColor="#6259b3" 
      > 
     </android.support.design.widget.TabLayout> 
    </android.support.v4.view.ViewPager> 

自定義佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/imageId_tabIcon" 
     android:layout_gravity="center" 
     /> 
</LinearLayout> 

請誰能幫助我在這個問題上我已經搜索的Android教程網站和搜索對谷歌太多,但沒有辦法解決那裏。 enter image description here

,如果我做的圖標大小24dp否則圖標會顯示類似下面的圖 enter image description here

+0

後您的自定義佈局。將tablayout重力設置爲'fill' – Piyush

+0

它被設置爲填充重力。但沒有工作。 和我編輯上面的代碼 – AAA

回答

0

變化

<ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/imageId_tabIcon" 
     android:layout_gravity="center" 
     /> 

<ImageView 
     android:layout_width="24dp" 
     android:layout_height="24dp" 
     android:id="@+id/imageId_tabIcon" 
     android:layout_gravity="center" 
     /> 

編輯答案開始 試試這個在您的自定義版式

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <ImageView 
     android:id="@+id/icon2" 
     android:layout_width="20dp" 
     android:layout_height="20dp" 
     android:layout_centerHorizontal="true" /> 

    <TextView 
     android:id="@+id/text1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/icon2" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="3dp" 
     android:textColor="@drawable/tab_text_color" 
     android:textSize="12dp" /> 

</RelativeLayout> 

和在遊碼

private void setupTabIcons() { 

     /*tabLayout.getTabAt(0).setIcon(R.drawable.icon_tab1_config); 
     tabLayout.getTabAt(1).setIcon(R.drawable.icon_tab2_config); 
     tabLayout.getTabAt(2).setIcon(R.drawable.icon_tab3_config); 
     tabLayout.getTabAt(3).setIcon(R.drawable.icon_tab4_config); 
     tabLayout.getTabAt(4).setIcon(R.drawable.icon_tab5_config);*/ 


     View view = LayoutInflater.from(this).inflate(R.layout.tab_indicator_two, null); 
     ((ImageView) view.findViewById(R.id.icon2)).setImageResource(R.drawable.icon_tab1_config); 


     View view2 = LayoutInflater.from(this).inflate(R.layout.tab_indicator_two, null); 
     ((ImageView) view2.findViewById(R.id.icon2)).setImageResource(R.drawable.icon_tab2_config); 


     View view3 = LayoutInflater.from(this).inflate(R.layout.tab_indicator_two, null); 
     ((ImageView) view3.findViewById(R.id.icon2)).setImageResource(R.drawable.icon_tab3_config); 


     View view4 = LayoutInflater.from(this).inflate(R.layout.tab_indicator_two, null); 
     ((ImageView) view4.findViewById(R.id.icon2)).setImageResource(R.drawable.icon_tab4_config); 


     View view5 = LayoutInflater.from(this).inflate(R.layout.tab_indicator_two, null); 
     ((ImageView) view5.findViewById(R.id.icon2)).setImageResource(R.drawable.icon_tab5_config); 


     tabLayout.getTabAt(0).setCustomView(view); 
     tabLayout.getTabAt(1).setCustomView(view2); 
     tabLayout.getTabAt(2).setCustomView(view3); 
     tabLayout.getTabAt(3).setCustomView(view4); 
     tabLayout.getTabAt(4).setCustomView(view5); 

    } 
+0

我編輯的問題,而我做你所說的 – AAA

+0

我已經編輯答案試試這個。希望你的問題能夠解決。 –

+0

相同的結果:(。,。,, – AAA