繁体   English   中英

如何在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"

希望它对你有用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM