【问题标题】:How to make tablayout fixed below action bar?如何使 tablayout 固定在操作栏下方?
【发布时间】:2018-05-29 13:36:35
【问题描述】:

我有以下布局,其中标签布局上方有一个图像滑块:

但我想将它固定在顶部,如下图所示:

这是我当前的代码:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <com.daimajia.slider.library.SliderLayout
        android:id="@+id/slider"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        />

    <android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        app:tabGravity="fill"
        android:layout_gravity="bottom"
        app:tabIndicatorColor="#000"
        app:tabMode="fixed"
        app:tabSelectedTextColor="@color/colorTextPrimary"
        app:tabTextColor="@color/colorTextDisable">

        <android.support.design.widget.TabItem
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="All"/>

        <android.support.design.widget.TabItem
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Casual"/>

        <android.support.design.widget.TabItem
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="WoMen"/>

        <android.support.design.widget.TabItem
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="New Jeans"/>

    </android.support.design.widget.TabLayout>

    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="800dp"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tablayout"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" >
    </android.support.v4.view.ViewPager>
</LinearLayout>

</ScrollView>

【问题讨论】:

    标签: android


    【解决方案1】:

    如果您想将TabLayout 向上移动,那么您需要 visibiltySliderLayout gone 或设置高度 SliderLayout0dp

    使用下面的代码,它将满足您在第二张图片中的要求。

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none"
        android:id="@+id/scrollView"
    >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
            <com.daimajia.slider.library.SliderLayout
                android:id="@+id/slider"
                android:layout_width="match_parent"
                android:layout_height="200dp" //set this value to 0dp or set the visibilty GONE HERE(if you just want to hide visibilty)
                />
    
            <android.support.design.widget.TabLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                app:tabGravity="fill"
                app:tabIndicatorColor="#000"
                app:tabMode="fixed">
    
                <android.support.design.widget.TabItem
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="All" />
    
                <android.support.design.widget.TabItem
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Casual" />
    
    
                <android.support.design.widget.TabItem
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="WoMen" />
    
                <android.support.design.widget.TabItem
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="New Jeans" />
    
            </android.support.design.widget.TabLayout>
    
            <android.support.v4.view.ViewPager
                android:layout_width="match_parent"
                android:layout_height="800dp" />
    
            <android.support.v4.view.ViewPager
                android:id="@+id/viewpager"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/tablayout"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"></android.support.v4.view.ViewPager>
        </LinearLayout>
    
    
    </ScrollView>
    

    然后以编程方式执行此操作

      scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
                @Override
                public void onScrollChanged() {
                    if (scrollView != null) {
                        if (scrollView.getChildAt(0).getBottom() <= (scrollView.getHeight() + scrollView.getScrollY())) {
                            slider.setVisibility(View.VISIBLE);
                        }
                        else {
                            slider.setVisibility(View.GONE);
                        }
                    }
                }
            });
    

    【讨论】:

    • 但在滚动时我想隐藏滑块
    • 在创建活动中
    • 我不明白在哪里添加这个代码块......请你帮忙
    • 在您的 java 活动类中,将上面的代码添加到 onResume 方法中,如下所示 ---------------------------- --------------- @Override protected void onResume() { super.onResume(); ScrollView scrollView = findViewById(R.id.scrollView); SliderLayout 滑块 = findViewById(R.id.slider); // 在此处添加上面的代码 }
    • 当我向上滚动时,它将固定在顶部,当我向下滚动时,滑块布局可能再次显示为我的第一张图片
    猜你喜欢
    • 2020-07-02
    • 2015-03-31
    • 1970-01-01
    • 2018-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多