2016-02-03 33 views
1

我正在使用片段中的tablayout。這是我的標籤Tablayout文字顏色不變,android?

public class TabFragment extends Fragment { 
private TabLayout tabLayout; 
private ViewPager viewPager; 

public TabFragment() { 
    // Required empty public constructor 
} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View v = inflater.inflate(R.layout.fragment_tab, container, false); 
    viewPager = (ViewPager) v.findViewById(R.id.viewpager); 
    setupViewPager(viewPager); 

    tabLayout = (TabLayout) v.findViewById(R.id.tabs); 
    tabLayout.setupWithViewPager(viewPager); 
    setupTabIcons(); 
    tabLayout.setSelectedTabIndicatorColor(ContextCompat.getColor(getActivity(), android.R.color.transparent)); 

    return v; 
} 

private void setupTabIcons() { 
    TextView tabOne = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.tab_text, null); 
    tabOne.setText("\"fragment 1\""); 
    tabLayout.getTabAt(0).setCustomView(tabOne); 

    TextView tabTwo = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.tab_text, null); 
    tabTwo.setText("\"fragment 2\""); 
    tabLayout.getTabAt(1).setCustomView(tabTwo); 
} 


private void setupViewPager(ViewPager viewPager) { 
    ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager()); 
    adapter.addFragment(new Fragment1(), "fragment 1"); 
    adapter.addFragment(new Fragment2(), "fragment 2"); 
    viewPager.setAdapter(adapter); 
} 

class ViewPagerAdapter extends FragmentPagerAdapter { 
    private final List<Fragment> mFragmentList = new ArrayList<>(); 
    private final List<String> mFragmentTitleList = new ArrayList<>(); 

    public ViewPagerAdapter(FragmentManager manager) { 
     super(manager); 
    } 

    @Override 
    public Fragment getItem(int position) { 
     return mFragmentList.get(position); 
    } 

    @Override 
    public int getCount() { 
     return mFragmentList.size(); 
    } 

    public void addFragment(Fragment fragment, String title) { 
     mFragmentList.add(fragment); 
     mFragmentTitleList.add(title); 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     return mFragmentTitleList.get(position); 
    } 
} 
} 

的片段XML的標籤

<?xml version="1.0" encoding="utf-8"?> 
<TextView 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:text="XXXX" 
android:textColor="@color/tab_text_color" 
android:gravity="center" 
android:textSize="18sp" 
android:layout_height="wrap_content" /> 

顏色選擇

<FrameLayout 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" 
tools:context="com.TabFragment"> 

<!-- TODO: Update blank fragment layout --> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <android.support.design.widget.TabLayout 
     android:id="@+id/tabs" 
     app:tabBackground="@drawable/tab_color_selector" 
     app:tabGravity="fill" 
     app:tabMode="fixed" 
     android:layout_margin="10dp" 
     android:background="@drawable/tab_rectangle" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/viewpager" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
</LinearLayout> 

</FrameLayout> 

文字主要碎片 - tab_text_color

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:color="@android:color/white" android:state_selected="true" /> 
<item android:color="@android:color/white" android:state_focused="true" /> 
<item android:color="@android:color/white" android:state_pressed="true" /> 
<item android:color="@color/colorAppBlue" /> 
</selector> 

當應用程序被打開了,我不會t選定的標籤文字顏色爲白色。

這是標籤選擇

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:drawable="@color/colorAppBlue" android:state_selected="true"/> 
<item android:drawable="@android:color/white"/> 
</selector> 
+0

應用樣式您tablayout <樣式名稱= 「ActPanelTabLayout.Theme」 父= 「Widget.Design.TabLayout」> <項目名稱= 「tabIndicatorColor」> @顏色/ memory_color_80 \ <項目名= 「機器人:文字顏色」> @顏色/ act_tab_black <項目名稱= 「tabSelectedTextColor」> @顏色/ act_tab_selector_text_color –

+0

@ SuhasB它不起作用。 – WISHY

+0

http://stackoverflow.com/questions/30921722/cannot-change-active-text-color-on-tablayout –

回答

0

也許,你的文字顏色是由代碼Android或失去了一些東西,我不能看到overrided。但我會給你另一種方式來使用這種方式來改變tablayout文本顏色。

<style name="TabLayoutTextAppearance" parent="TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse"> 
    <item name="android:textSize">14sp</item> 
    <item name="android:textColor">//your text color</item> 
    <item name="android:textAllCaps">true</item> 
</style> 

而且這種風格添加到您的tablayout

app:tabTextAppearance="@style/TabLayoutTextAppearance" 
+1

它不起作用 – WISHY

+0

這些代碼段工作得很好在我目前的項目上。請先用乾淨的方式再次嘗試,而不需要先定製任何東西在成功之後,你可以用這個@WISHY做更多的事情 – topcbl

1

試試這個:

在onCreateView

tableLayout.getChildAt(0).setSelected(真);

+0

這是一個TabLayout而不是TableLayout – WISHY

+0

對不起,必須先選擇一個標籤,例如:tabLayout.getTabAt(0).select(); – Sabari

+0

它也沒有工作 – WISHY

1

最後解決它 只是需要加入這一行....

tabLayout.getTabAt(0).getCustomView().setSelected(true);