如何在 Android 中更改小部件 TabLayout 指示器的重力
How to change gravity of indicator of widget TabLayout in Android
这里我想把TabIndicator
的位置从下往上改,但是没有成功。我想要的就像图像显示的那样。
我已经完成了所有工作,但指示器位于底部而不是顶部,甚至我也无法在选项卡之间添加分隔线。请查看我的代码并帮助我解决此问题。
我的代码是这样的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.cws.widget.MyTabLayout
android:id="@+id/mainTabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
app:tabBackground="@color/drawer_tab_background"
app:tabGravity="fill"
app:tabIndicatorColor="@color/green"
app:tabMode="fixed" />
<com.cws.widget.NonSiwpablePager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
和styles.xml
<style name="AppCompatTheme" parent="Theme.AppCompat.Light.NoActionBar"></style>
<style name="AppToolbarTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:windowNoTitle">true</item>
<item name="colorPrimary">#000</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="colorControlNormal">#FFF</item>
</style>
MyTabLayout.java
public class MyTabLayout extends TabLayout {
public MyTabLayout(Context context) {
super(context);
}
public MyTabLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int height = 0;
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
child.measure(widthMeasureSpec, View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
int h = child.getMeasuredHeight();
if (h > height) height = h;
}
heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
ViewPager.java
public class NonSiwpablePager extends ViewPager {
public NonSiwpablePager(Context context) {
super(context);
}
public NonSiwpablePager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return false;
}
}
首先你需要删除那个标签指示器,
app:tabIndicatorHeight="0dp"
此行添加到 <com.cws.widget.MyTabLayout>
xml
文件中。然后添加 http://pastie.org/private/5lsxm7l8qoay4naobuddow 作为背景
app:tabBackground="@drawable/selector_tab"
希望对你有用
这里我想把TabIndicator
的位置从下往上改,但是没有成功。我想要的就像图像显示的那样。
我已经完成了所有工作,但指示器位于底部而不是顶部,甚至我也无法在选项卡之间添加分隔线。请查看我的代码并帮助我解决此问题。
我的代码是这样的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.cws.widget.MyTabLayout
android:id="@+id/mainTabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
app:tabBackground="@color/drawer_tab_background"
app:tabGravity="fill"
app:tabIndicatorColor="@color/green"
app:tabMode="fixed" />
<com.cws.widget.NonSiwpablePager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
和styles.xml
<style name="AppCompatTheme" parent="Theme.AppCompat.Light.NoActionBar"></style>
<style name="AppToolbarTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:windowNoTitle">true</item>
<item name="colorPrimary">#000</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="colorControlNormal">#FFF</item>
</style>
MyTabLayout.java
public class MyTabLayout extends TabLayout {
public MyTabLayout(Context context) {
super(context);
}
public MyTabLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int height = 0;
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
child.measure(widthMeasureSpec, View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
int h = child.getMeasuredHeight();
if (h > height) height = h;
}
heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
ViewPager.java
public class NonSiwpablePager extends ViewPager {
public NonSiwpablePager(Context context) {
super(context);
}
public NonSiwpablePager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return false;
}
}
首先你需要删除那个标签指示器,
app:tabIndicatorHeight="0dp"
此行添加到 <com.cws.widget.MyTabLayout>
xml
文件中。然后添加 http://pastie.org/private/5lsxm7l8qoay4naobuddow 作为背景
app:tabBackground="@drawable/selector_tab"
希望对你有用