我希望在选中它时更改标签的颜色。在他们用tabSelectedTextColor
设置的matrial设计教程中我们可以做到这一点。但它对我不起作用。我强迫onTabSelected
代码如下:
public void onTabSelected(TabLayout.Tab tab) {
TextView v = (TextView) tab.getCustomView().findViewById(R.id.text_tab_counter);
v.setVisibility(View.INVISIBLE);
TextView vv=(TextView) tab.getCustomView().findViewById(R.id.text_tab);
vv.setTextColor(getResources().getColor(R.color.Notify));
v.setText("");
tab.getCustomView().invalidate();
viewPager.setCurrentItem(tab.getPosition());
}
为什么这不起作用?
修改 这是因为我有自定义标签布局吗?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
>
<TextView
android:textColor="@color/textColorPrimary"
android:layout_marginRight="2dp"
android:id="@+id/text_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:text="•"
android:textColor="@color/Notify"
android:textSize="@dimen/badget_size"
android:id="@+id/text_tab_counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
编辑:
ViewPageAdapter
代码:
package ir.whc.news.adapter;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import ir.whc.news.R;
/**
* Created by marzieh on 3/31/2016.
*/
public class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
private Context context;
public ViewPagerAdapter(FragmentManager manager, Context context) {
super(manager);
this.context = context;
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
public View getTabView(int position,boolean havenew) {
View v = LayoutInflater.from(context).inflate(R.layout.custom_tab, null);
TextView tv = (TextView) v.findViewById(R.id.text_tab);
TextView tv2= (TextView) v.findViewById(R.id.text_tab_counter);
tv.setText(getPageTitle(position));
tv.setTextColor(context.getResources().getColor(R.color.textColorPrimary));
//tv2.setText(String.valueOf(count));
//tv2.setVisibility(havenew ? View.VISIBLE : View.INVISIBLE);
tv2.setText(havenew?context.getString(R.string.newBadgerSign):"");
tv2.setTextColor(context.getResources().getColor(R.color.Notify));
return v;
}
}
答案 0 :(得分:2)
在Material TabLayout中添加:
app:tabTextColor="@color/normal_color"
app:tabSelectedTextColor="@color/selected_color"
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabTextAppearance="@style/CustomTabText"
app:tabTextColor="@color/text_color"
app:tabSelectedTextColor="@color/text_color"
app:tabMode="fixed" />
对于自定义TabLayout,请在getView()方法中添加:
tabTitleView.setTextColor(getResources().getColorStateList(R.drawable.selector_textview))
;
在drawable selector_textview.xml中添加:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_selected="true" android:color="@color/selected_color" />
<item android:state_focused="true" android:color="@color/selected_color" />
<item android:state_pressed="true" android:color="@color/selected_color" />
<item android:color="@color/normal_color" />
</selector>