gpt4 book ai didi

android - 设置选项卡选定的文本颜色不起作用

转载 作者:行者123 更新时间:2023-11-29 17:20:36 25 4
gpt4 key购买 nike

我想在选中时更改选项卡的颜色。在 Material 设计教程中,他们说通过设置 tabSelectedTextColor 我们可以做到这一点。但它对我不起作用。我在 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;
}

}

最佳答案

在 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));

将此添加到您的可绘制对象 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>

关于android - 设置选项卡选定的文本颜色不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36710763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com