获取 TabLayout 选项卡的文本视图
Getting the textview of the TabLayout's tab
我想以编程方式更改选项卡的文本视图。有什么办法吗?
只有关于旧 TabHost 视图的答案,我正在使用 Google 的 Material 设计库使用的 TabLayout 使用 android.support.design.widget.TabLayout.
对于 TabHost:
How to add padding to a tabs label?
对于给定的选项卡:
int wantedTabIndex = 0;
TextView tv = (TextView)(((LinearLayout)((LinearLayout)mTabLayout.getChildAt(0)).getChildAt(wantedTabIndex)).getChildAt(0));
tv.setText("Hello world!");
public static void setTextViewsCapsOff(View view) {
if (!(view instanceof ViewGroup)) {
return;
}
ViewGroup group = (ViewGroup)view;
for (int i = 0; i < group.getChildCount(); i++) {
View child = group.getChildAt(i);
if (child instanceof TextView) {
((TextView)child).setAllCaps(false);
} else {
setTextViewsCapsOff(child);
}
}
}
将您的 TabLayout 传递给此递归方法。它会找到任何 child 的 TextView 并关闭其全部大写模式。避免所有其他非常具体的类型转换。如果它似乎不起作用,请稍后在您的代码中调用它。我在 onCreate 中有它,但那没有用。稍后在代码中调用它并且它完美地工作。
影响所有选项卡,不只是一个选项卡,但我认为这是最常见的用法。也不特定于 TabLayout。可用于包含您要更改的 TextView 的任何布局。
我认为最好的做法是使用 TextView 创建自定义布局,然后将自定义视图分配给您要编辑的选项卡
final TabLayout.Tab tabAt = tabLayout.getTabAt(index);
tabAt.setCustomView(myLayoutId);
通过这种方式,您可以使用布局内的 TextView 做任何您想做的事情
这是可行的解决方案
int wantedTabIndex = 0;
TextView tv = (TextView)(((LinearLayout)((LinearLayout)mTabLayout.getChildAt(0)).getChildAt(wantedTabIndex)).getChildAt(1));
tv.setText("Hello world!");
只需将最后一个索引 Zero 更改为 One 现在上面的代码就可以工作了
移除崩溃
java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.TextView
将 onTabSelectedListener 添加到您的 tabLayout 以动态设置文本。
因此,在此方法中,选择选项卡时文本将变为"selected"或其他选项卡
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
tab.getCustomView().setAlpha(1.0f);
((TextView) tab.getCustomView()).setTextSize(12);
((TextView) tab.getCustomView()).setText("Selected");
Log.d("TabBar", "selected");
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
tab.getCustomView().setAlpha(0.3f);//to set alpha
((TextView) tab.getCustomView()).setTextSize(11);
((TextView) tab.getCustomView()).setText("DeSelected");
Log.d("TabBar", "unselected");
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
Log.d("TabBar", "reselected");
}
});
将选项卡设置为默认选中然后使用此语句
tabLayout.getTabAt(0).select(); //this is used to set tab as default
这对我有用:
tabLayout.getTabAt(0).setText("some text");
将 0
替换为您要编辑的选项卡的索引。我认为您的 tabLayout 需要在调用之前填充,否则 tablayout.getTabAt(0)
将 return null
.
凯文的回答很棒! ()。依靠这个答案,我创建了在 Kotlin
:
中为 TextView
应用任何更改的实现
private fun applyForTextView(parent: ViewGroup, func: (TextView) -> Unit) {
for (i in 0 until parent.childCount) {
when (val child = parent.getChildAt(i)) {
is TextView -> func(child)
is ViewGroup -> applyForTextView(child, func)
}
}
}
使用方法:
applyForTextView(tabLayout) { it.isAllCaps = false }
我想以编程方式更改选项卡的文本视图。有什么办法吗?
只有关于旧 TabHost 视图的答案,我正在使用 Google 的 Material 设计库使用的 TabLayout 使用 android.support.design.widget.TabLayout.
对于 TabHost: How to add padding to a tabs label?
对于给定的选项卡:
int wantedTabIndex = 0;
TextView tv = (TextView)(((LinearLayout)((LinearLayout)mTabLayout.getChildAt(0)).getChildAt(wantedTabIndex)).getChildAt(0));
tv.setText("Hello world!");
public static void setTextViewsCapsOff(View view) {
if (!(view instanceof ViewGroup)) {
return;
}
ViewGroup group = (ViewGroup)view;
for (int i = 0; i < group.getChildCount(); i++) {
View child = group.getChildAt(i);
if (child instanceof TextView) {
((TextView)child).setAllCaps(false);
} else {
setTextViewsCapsOff(child);
}
}
}
将您的 TabLayout 传递给此递归方法。它会找到任何 child 的 TextView 并关闭其全部大写模式。避免所有其他非常具体的类型转换。如果它似乎不起作用,请稍后在您的代码中调用它。我在 onCreate 中有它,但那没有用。稍后在代码中调用它并且它完美地工作。
影响所有选项卡,不只是一个选项卡,但我认为这是最常见的用法。也不特定于 TabLayout。可用于包含您要更改的 TextView 的任何布局。
我认为最好的做法是使用 TextView 创建自定义布局,然后将自定义视图分配给您要编辑的选项卡
final TabLayout.Tab tabAt = tabLayout.getTabAt(index);
tabAt.setCustomView(myLayoutId);
通过这种方式,您可以使用布局内的 TextView 做任何您想做的事情
这是可行的解决方案
int wantedTabIndex = 0;
TextView tv = (TextView)(((LinearLayout)((LinearLayout)mTabLayout.getChildAt(0)).getChildAt(wantedTabIndex)).getChildAt(1));
tv.setText("Hello world!");
只需将最后一个索引 Zero 更改为 One 现在上面的代码就可以工作了
移除崩溃
java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.TextView
将 onTabSelectedListener 添加到您的 tabLayout 以动态设置文本。 因此,在此方法中,选择选项卡时文本将变为"selected"或其他选项卡
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
tab.getCustomView().setAlpha(1.0f);
((TextView) tab.getCustomView()).setTextSize(12);
((TextView) tab.getCustomView()).setText("Selected");
Log.d("TabBar", "selected");
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
tab.getCustomView().setAlpha(0.3f);//to set alpha
((TextView) tab.getCustomView()).setTextSize(11);
((TextView) tab.getCustomView()).setText("DeSelected");
Log.d("TabBar", "unselected");
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
Log.d("TabBar", "reselected");
}
});
将选项卡设置为默认选中然后使用此语句
tabLayout.getTabAt(0).select(); //this is used to set tab as default
这对我有用:
tabLayout.getTabAt(0).setText("some text");
将 0
替换为您要编辑的选项卡的索引。我认为您的 tabLayout 需要在调用之前填充,否则 tablayout.getTabAt(0)
将 return null
.
凯文的回答很棒! (Kotlin
:
TextView
应用任何更改的实现
private fun applyForTextView(parent: ViewGroup, func: (TextView) -> Unit) {
for (i in 0 until parent.childCount) {
when (val child = parent.getChildAt(i)) {
is TextView -> func(child)
is ViewGroup -> applyForTextView(child, func)
}
}
}
使用方法:
applyForTextView(tabLayout) { it.isAllCaps = false }