2015-11-14 230 views
0

我需要你的幫助。我跟着這個樣本hereAndroid TabLayout不顯示文本,只顯示圖標

所有工作除文字以外只顯示圖標。

這裏是我的代碼:

MainActivity.class

private int[] ICONS = { 
     R.drawable.tab_icon_home, R.drawable.tab_icon_heart, 
     R.drawable.tab_icon_news, R.drawable.tab_icon_profile 
}; 

private final String[] TITLES = {"Home", "Shortlist", "Feed" , "Profile"}; 

ViewPager mViewpager; 
TabLayout mTabs; 
private FragmentPageAdapter pageAdapter; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    mTabs = (TabLayout) findViewById(R.id.tabs); 
    mViewpager = (ViewPager) findViewById(R.id.viewpager); 

    setupViewPager(mViewpager); 
    setupTabLayout(mTabs); 

} 

public void setupViewPager(ViewPager viewPager) { 
    pageAdapter = new FragmentPageAdapter(getApplicationContext(), getSupportFragmentManager()); 
    pageAdapter.addFragment(CardFragment.getInstance(TITLES[0]), TITLES[0], ICONS[0]); 
    pageAdapter.addFragment(CardFragment.getInstance(TITLES[1]), TITLES[1], ICONS[1]); 
    pageAdapter.addFragment(CardFragment.getInstance(TITLES[2]), TITLES[2], ICONS[2]); 
    pageAdapter.addFragment(CardFragment.getInstance(TITLES[3]), TITLES[3], ICONS[3]); 
    viewPager.setAdapter(pageAdapter); 
} 

public void setupTabLayout(TabLayout tabLayout) { 
    tabLayout.setTabMode(TabLayout.MODE_FIXED); 
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); 
    tabLayout.setupWithViewPager(mViewpager); 


    for (int i = 0; i < tabLayout.getTabCount(); i++) { 
     TabLayout.Tab tab = tabLayout.getTabAt(i); 
     tabLayout.getTabAt(i).setText(TITLES[i]); 
     tab.setCustomView(pageAdapter.getTabView(i)); 
    } 
    tabLayout.requestFocus(); 
} 

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MainActivity"> 

<FrameLayout 
    android:id="@+id/flContainer" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_above="@+id/tabs" 
    android:layout_alignParentTop="true"> 


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

    <View 
     android:id="@+id/shadowView" 
     android:layout_width="match_parent" 
     android:layout_height="4dp" 
     android:layout_gravity="bottom" 
     android:background="@color/white" 
     android:rotation="180" /> 

</FrameLayout> 

<android.support.design.widget.TabLayout 
    android:id="@+id/tabs" 
    android:background="@color/white" 
    style="@style/MyCustomTabLayout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

CardFragme nt.class:

public static CardFragment newInstance(int position) { 
    CardFragment f = new CardFragment(); 
    Bundle b = new Bundle(); 
    b.putInt(ARG_POSITION, position); 
    f.setArguments(b); 
    return f; 
} 

public static CardFragment getInstance(String message) { 
    CardFragment mainFragment = new CardFragment(); 
    Bundle bundle = new Bundle(); 
    bundle.putString("MSG", message); 
    mainFragment.setArguments(bundle); 
    return mainFragment; 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    position = getArguments().getInt(ARG_POSITION); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.fragment_card,container,false); 
    ViewCompat.setElevation(rootView, 50); 
    return rootView; 
} 

你覺得在我的代碼缺失?我卡住了,不知道如何解決這個問題。任何幫助將不勝感激。謝謝!

+0

顯示'@ρяσѕρєяK我已經添加它CardFragment'類代碼 –

+0

。 PLS。檢查謝謝! –

回答

0

如同提供代碼鏈接一樣,也可以調用setText設置當前Fragment標題的方法。

而且目前通過調用getInstance字符串版本越來越片段實例,因此使用的getString爲onCreateView獲得標題:

... 
ViewCompat.setElevation(rootView, 50); 
TextView textTitle=(TextView)rootView.findViewById(R.id.<textview_id>); 
textTitle.setText(""+ getArguments().getString("Bundle")); 
return rootView;