【问题标题】:How to change gravity of indicator of widget TabLayout in Android如何在Android中更改小部件TabLayout指示器的重力
【发布时间】:2016-04-14 17:18:57
【问题描述】:

在这里,我试图将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;
}
}

【问题讨论】:

  • 你在使用自定义tablayout吗??
  • 显然。但我对此感到困惑
  • 请同时更新该课程。或者您可以使用不同的图像并更改选择取消选择
  • 请再次检查。这是图像和标题的选择器之一pastie.org/private/5lsxm7l8qoay4naobuddow

标签: android viewpagerindicator android-tablayout


【解决方案1】:

首先你需要删除那个标签指示器,

app:tabIndicatorHeight="0dp"

此行添加到 &lt;com.cws.widget.MyTabLayout&gt; xml 文件中。然后添加 http://pastie.org/private/5lsxm7l8qoay4naobuddow 作为背景

app:tabBackground="@drawable/selector_tab"

希望对你有用

【讨论】:

  • tabBackground改变了Tab的完整背景
  • 是的,它会改变整个背景。所以你可以根据它改变图像
  • 我已经这样做了。你的意思是我现在必须要完整的图片。
  • 图像是更好的选择,否则您将如何管理文本和图标颜色...如果您可以管理,则使用颜色选项
  • 是的,我知道我也在为文本和图标这样做。现在我要求提供新图像并且它起作用了。
猜你喜欢
  • 2016-11-26
  • 2021-12-11
  • 2016-02-21
  • 1970-01-01
  • 1970-01-01
  • 2015-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多