我想减小指示器的宽度 - 例如,使其宽度不是整个标签宽度,而是标签宽度的1/2。 这就是我需要做的全部,所以我不想下载一些自定义库并搜索我能做到的地方。
这是一种方法吗?或者我应该自己写这样的观点?
答案 0 :(得分:4)
对于那些最终遇到此问题的人,我发现了一个简单的解决方案,其中的矢量可绘制对象占制表符宽度指示器的大约90%:
ic_tab_indicator_24dp:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row middle-xs center-xs ">
<fieldset class="col-xs-12 col-md-6">
<div class="input-set">
<span class="label">Company Name</span>
<input name="MerchantName" class="bp-company-name-field kyc-flag" placeholder="Company name" type="text" value="Brand P" title="Company Name" disabled="" required="">
<span class="kycFlagIcon bp-company-name-field">!</span>
</div>
</fieldset>
<fieldset class="col-xs-12 col-md-6">
<div class="input-set">
<span class="label">Username</span>
<input name="UserName" placeholder="Username" class="bp-username-field valid" type="text" value="Branded" required="" data-rule-minlength="2" data-rule-maxlength="255" aria-invalid="false">
</div>
</fieldset>
</div>
然后在布局中设置tabIndicator:
<vector
android:height="24dp"
android:width="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0"
xmlns:android="http://schemas.android.com/apk/res/android">
<path
android:strokeWidth="4"
android:fillColor="@android:color/white"
android:pathData="M2,0 L22,0 L22,24 L2,24 z"/>
或在styles.xml中:
app:tabIndicator="@drawable/ic_tab_indicator_24dp"
答案 1 :(得分:3)
试试这个。
public void setIndicator (TabLayout tabs,int leftDip,int rightDip){
Class<?> tabLayout = tabs.getClass();
Field tabStrip = null;
try {
tabStrip = tabLayout.getDeclaredField("mTabStrip");
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
tabStrip.setAccessible(true);
LinearLayout llTab = null;
try {
llTab = (LinearLayout) tabStrip.get(tabs);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());
for (int i = 0; i < llTab.getChildCount(); i++) {
View child = llTab.getChildAt(i);
child.setPadding(0, 0, 0, 0);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
params.leftMargin = left;
params.rightMargin = right;
child.setLayoutParams(params);
child.invalidate();
}
}
然后
tab.post(new Runnable() {
@Override
public void run() {
setIndicator(tab,60,60);
}
});
我的修改没有反射(应该设置自定义视图!)。
for (int i = 0; i < tabs.getTabCount(); i++) {
TabLayout.Tab tab = tabs.getTabAt(i);
if (tab != null) {
View customView = tab.getCustomView();
if (customView != null) {
View targetViewToApplyMargin = (View) customView.getParent();
ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) targetViewToApplyMargin.getLayoutParams();
layoutParams.rightMargin = totalTabMargin;
targetViewToApplyMargin.setLayoutParams(layoutParams);
}
}
}
答案 2 :(得分:1)
你可以复制TabLayout,并修改计算指标宽度的逻辑。请参阅updateIndicatorPosition()和animateIndicatorToPosition()。 一个简单的演示https://github.com/xybean/CustomTabLayout
答案 3 :(得分:1)
不知道固定大小的选项卡指示器是否要实现,但这是我想做的,但无法在线找到。
要创建固定大小的指标形状,我创建了以下可绘制对象:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:gravity="center">
<shape android:shape="rectangle">
<solid android:color="@color/colorAccent" />
<corners
android:topLeftRadius="2dp"
android:topRightRadius="2dp" />
<size
android:width="80dp"
android:height="2dp" />
</shape>
</item>
</layer-list>
然后可以在tabLayout本身上设置可绘制的标签指示符:app:tabIndicator="@drawable/tab_indicator"
。由于可绘制对象本身的形状在其边界内居中,因此指示器将具有固定的大小。
如果您希望指示器与标签的宽度匹配,则可以使用app:tabIndicatorFullWidth="false"
,但这对我来说确实不够。我找不到增加该宽度填充的方法。
答案 4 :(得分:0)
尝试了所有这些解决方案,但不满意,最后我自己找到了一个解决方案,我们只需要修改SlidingTabIndicator的indicatorLeft和indicatorRight文件,扩展tablayout并在需要时调用setIndicaotrWidth(),例如:
setIndicatorWidth(70);
整个代码:
import android.content.Context;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.google.android.material.tabs.TabLayout;
import java.lang.reflect.Field;
import androidx.annotation.NonNull;
public class TabLayoutEx extends TabLayout {
public TabLayoutEx(Context context) {
this(context, null);
}
public TabLayoutEx(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public TabLayoutEx(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(attrs);
}
private void init(AttributeSet attrs) {
setTabIndicatorFullWidth(false);
setIndicatorWidth(70);
}
private class DefPreDrawListener implements ViewTreeObserver.OnPreDrawListener {
private LinearLayout tabStrip = null;
private int tabWidth;
private Field fieldLeft;
private Field fieldRight;
public void setTabStrip(LinearLayout tabStrip, int width) {
try {
this.tabStrip = tabStrip;
this.tabWidth = width;
Class cls = tabStrip.getClass();
fieldLeft = cls.getDeclaredField("indicatorLeft");
fieldLeft.setAccessible(true);
fieldRight = cls.getDeclaredField("indicatorRight");
fieldRight.setAccessible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public boolean onPreDraw() {
try {
if (tabWidth > 0) {
int left = fieldLeft.getInt(this.tabStrip);
int right = fieldRight.getInt(this.tabStrip);
//根据目标宽度及现在的宽度调整为合适的left和right
int diff = right - left - tabWidth;
left = left + diff / 2;
right = right - diff / 2;
fieldLeft.setInt(this.tabStrip, left);
fieldRight.setInt(this.tabStrip, right);
}
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
}
private DefPreDrawListener defPreDrawListener = new DefPreDrawListener();
public void setIndicatorWidth(int widthDp) {
Class<?> tabLayout = TabLayout.class;
Field tabStrip = null;
try {
tabStrip = tabLayout.getDeclaredField("slidingTabIndicator");
tabStrip.setAccessible(true);
LinearLayout tabIndicator = (LinearLayout) tabStrip.get(this);
int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, widthDp, Resources.getSystem().getDisplayMetrics());
//avoid add preDrawListener multi times
tabIndicator.getViewTreeObserver().removeOnPreDrawListener(defPreDrawListener);
tabIndicator.getViewTreeObserver().addOnPreDrawListener(defPreDrawListener);
defPreDrawListener.setTabStrip(tabIndicator, width);
} catch (Exception e) {
e.printStackTrace();
}
}
}