我正在使用SlidingTabLayout,我想从右向左滑动选项卡,但我找不到办法。
我已经尝试过'setGravity()'方法,但是它不起作用。
也许您需要尝试布局镜像:
要利用RTL布局镜像,只需对您的应用进行以下更改:
在您的应用程序清单中声明您的应用程序支持RTL镜像。具体来说,将android:supportsRtl =“ true”添加到清单文件中的元素。
将应用程序的所有“左/右”布局属性更改为新的“开始/结束”等效项。如果您将应用程序定位为Android 4.2(应用程序的targetSdkVersion或minSdkVersion为17或更高),则应使用“开始”和“结束”,而不是“左”和“右”。例如,android:paddingLeft应该变成android:paddingStart。如果要让您的应用程序使用Android 4.2之前的版本(应用程序的targetSdkVersion或minSdkVersion为16或更小),则除了“ left”和“ right”之外,还应添加“ start”和“ end”。例如,您将同时使用android:paddingLeft和android:paddingStart。
我已尝试通过此方法解决此问题:
private void scrollToTab(int tabIndex, int positionOffset) {
if (isRtlMode()) {
scrollToTabRtl(tabIndex, positionOffset);
} else {
scrollToLtr(tabIndex, positionOffset);
}
}
private void scrollToTabRtl(int tabIndex, int positionOffset) {
final int maxItemCount = mTabStrip.getChildCount();
if (maxItemCount == 0 || tabIndex < 0 || tabIndex >= maxItemCount) {
return;
}
View activeItemView = mTabStrip.getChildAt(tabIndex);
if (activeItemView != null) {
int totalWidth = getWidth();
int targetScroll = totalWidth - activeItemView.getRight() + positionOffset;
super.smoothScrollTo(-targetScroll, 0);
}
}