gpt4 book ai didi

Android 选项卡布局 : Wrap tab indicator width with respect to tab title

转载 作者:IT老高 更新时间:2023-10-28 22:20:35 33 4
gpt4 key购买 nike

有什么方法可以根据标签标题包装标签指示器的宽度吗?

Current

Required

最佳答案

是的,可以将标签指示符作为标题设置填充到 0

 public void wrapTabIndicatorToTitle(TabLayout tabLayout, int externalMargin, int internalMargin) {
View tabStrip = tabLayout.getChildAt(0);
if (tabStrip instanceof ViewGroup) {
ViewGroup tabStripGroup = (ViewGroup) tabStrip;
int childCount = ((ViewGroup) tabStrip).getChildCount();
for (int i = 0; i < childCount; i++) {
View tabView = tabStripGroup.getChildAt(i);
//set minimum width to 0 for instead for small texts, indicator is not wrapped as expected
tabView.setMinimumWidth(0);
// set padding to 0 for wrapping indicator as title
tabView.setPadding(0, tabView.getPaddingTop(), 0, tabView.getPaddingBottom());
// setting custom margin between tabs
if (tabView.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) tabView.getLayoutParams();
if (i == 0) {
// left
settingMargin(layoutParams, externalMargin, internalMargin);
} else if (i == childCount - 1) {
// right
settingMargin(layoutParams, internalMargin, externalMargin);
} else {
// internal
settingMargin(layoutParams, internalMargin, internalMargin);
}
}
}

tabLayout.requestLayout();
}
}

private void settingMargin(ViewGroup.MarginLayoutParams layoutParams, int start, int end) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
layoutParams.setMarginStart(start);
layoutParams.setMarginEnd(end);
layoutParams.leftMargin = start;
layoutParams.rightMargin = end;
} else {
layoutParams.leftMargin = start;
layoutParams.rightMargin = end;
}
}

Result

编辑:从 com.android.support:design:28.0.0 开始,您现在可以轻松地将指标调整为标签:

app:tabIndicatorFullWidth="false"

2019 年 7 月编辑:使用 ma​​terial 依赖com.google.android.material:material:x.x.x

关于Android 选项卡布局 : Wrap tab indicator width with respect to tab title,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40480675/

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