setCustomView不适用于android studio中的TableLayout
我有一个带有5个选项卡的setCustomView不适用于android studio中的TableLayout,android,android-tablayout,Android,Android Tablayout,我有一个带有5个选项卡的tablayout。每个选项卡都有一个图标和标题。我想更改选中选项卡和未选中选项卡的图标和标题的颜色。为此,我尝试使用自定义选项卡,但不起作用。这是我的自定义选项卡.xml代码: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_widt
tablayout
。每个选项卡都有一个图标
和标题
。我想更改选中选项卡和未选中选项卡的图标和标题的颜色。为此,我尝试使用自定义选项卡,但不起作用。这是我的自定义选项卡.xml
代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center"
android:id="@+id/tabIcon"/>
<com.prj.shopt.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#C1C1C1"
android:gravity="center"
android:id="@+id/tabTitle"/>
</LinearLayout>
import android.content.Context;
import android.support.design.widget.TabLayout;
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.ImageView;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.List;
public class HomeTabsAdapter extends FragmentPagerAdapter {
List<Fragment> fragments;
List<String> tabTitles;
public HomeTabsAdapter(FragmentManager fm) {
super(fm);
fragments=new ArrayList<>();
tabTitles=new ArrayList<>();
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
public void addFragment(Fragment fragment,String title){
fragments.add(fragment);
tabTitles.add(title);
notifyDataSetChanged();
}
public static View getTabView(int imageId, int color, String title, Context context, TabLayout tabLayout){
View v= LayoutInflater.from(context).inflate(R.layout.custom_tab,tabLayout,false);
TextView tabTitle=v.findViewById(R.id.tabTitle);
tabTitle.setText(title);
tabTitle.setTextColor(color);
tabTitle.setTypeface(Utility.getFontTypeFace(context));
ImageView icon=(v.findViewById(R.id.tabIcon));
icon.setImageResource(0);
icon.setImageResource(imageId);
return v;
}
@Override
public CharSequence getPageTitle(int position) {
return tabTitles.get(position);
}
}
这是我的适配器(HomeTabsAdapter.java
):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center"
android:id="@+id/tabIcon"/>
<com.prj.shopt.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#C1C1C1"
android:gravity="center"
android:id="@+id/tabTitle"/>
</LinearLayout>
import android.content.Context;
import android.support.design.widget.TabLayout;
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.ImageView;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.List;
public class HomeTabsAdapter extends FragmentPagerAdapter {
List<Fragment> fragments;
List<String> tabTitles;
public HomeTabsAdapter(FragmentManager fm) {
super(fm);
fragments=new ArrayList<>();
tabTitles=new ArrayList<>();
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
public void addFragment(Fragment fragment,String title){
fragments.add(fragment);
tabTitles.add(title);
notifyDataSetChanged();
}
public static View getTabView(int imageId, int color, String title, Context context, TabLayout tabLayout){
View v= LayoutInflater.from(context).inflate(R.layout.custom_tab,tabLayout,false);
TextView tabTitle=v.findViewById(R.id.tabTitle);
tabTitle.setText(title);
tabTitle.setTextColor(color);
tabTitle.setTypeface(Utility.getFontTypeFace(context));
ImageView icon=(v.findViewById(R.id.tabIcon));
icon.setImageResource(0);
icon.setImageResource(imageId);
return v;
}
@Override
public CharSequence getPageTitle(int position) {
return tabTitles.get(position);
}
}
导入android.content.Context;
导入android.support.design.widget.TabLayout;
导入android.support.v4.app.Fragment;
导入android.support.v4.app.FragmentManager;
导入android.support.v4.app.FragmentPagerAdapter;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.widget.ImageView;
导入android.widget.TextView;
导入org.w3c.dom.Text;
导入java.util.ArrayList;
导入java.util.List;
公共类HomeTabsAdapter扩展了FragmentPagerAdapter{
列出片段;
列出标题;
公共HomeTabsAdapter(碎片管理器fm){
超级(fm);
片段=新的ArrayList();
tabTitles=newarraylist();
}
@凌驾
公共片段getItem(int位置){
返回碎片。获取(位置);
}
@凌驾
public int getCount(){
返回fragments.size();
}
public void addFragment(片段片段,字符串标题){
片段。添加(片段);
tabTitles.add(title);
notifyDataSetChanged();
}
公共静态视图getTabView(int-imageId、int-color、字符串标题、上下文上下文、TabLayout-TabLayout){
视图v=LayoutFlater.from(上下文)。充气(R.layout.custom_选项卡,TableLayout,false);
TextView tabTitle=v.findviewbyd(R.id.tabTitle);
tabTitle.setText(标题);
tabTitle.setTextColor(颜色);
tabTitle.setTypeface(实用程序.getFontTypeFace(上下文));
ImageView图标=(v.findViewById(R.id.tabIcon));
icon.setImageResource(0);
icon.setImageResource(imageId);
返回v;
}
@凌驾
公共字符序列getPageTitle(int位置){
返回tabTitles.get(位置);
}
}
结果是图标颜色固定,标题颜色固定。选择和取消选择选项卡时不会发生任何更改。
感谢您的帮助使用此方法:-
private void setCustomTab(TabLayout tabLayout, int index, String title) {
TextView tab_home = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.custome_tab, null);
tab_home.setText(title);
tabLayout.getTabAt(index).setCustomView(tab_home);
}
使用上述方法,如下所示:-
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
setCustomTab(tabLayout, 0, "Upcoming");
setCustomTab(tabLayout, 1, "Completed");
在HomeTabsAdapter中使用此方法和getTabView方法有什么区别?