首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在android中隐藏滚动条底部的应用程序栏

如何在android中隐藏滚动条底部的应用程序栏
EN

Stack Overflow用户
提问于 2019-03-18 14:43:17
回答 4查看 3.4K关注 0票数 2

我正在制作一个安卓应用程序,其中有一个活动和其他活动实现了主要的activity.Now,我还实现了一个活动,多个片段pattern.So,每个活动至少有7-8个片段。这是我的主要活动的布局。

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
    tools:context=".MainActivity">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="60dp"
    android:id="@+id/frame_lay">

</FrameLayout>



<View
    android:layout_width="match_parent"
    android:layout_height="4dp"
    android:layout_above="@id/bottom_appbar"
    app:layout_anchor="@+id/bottom_appbar"

    android:background="@android:color/darker_gray"/>

<com.google.android.material.bottomappbar.BottomAppBar
    android:id="@+id/bottom_appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    app:backgroundTint="@color/colorbottomappbar"
    app:fabAlignmentMode="center"
    app:navigationIcon="@drawable/ic_menu_green_24dp">

</com.google.android.material.bottomappbar.BottomAppBar>



<ImageButton
    android:id="@+id/fab"
    android:layout_width="190dp"
    android:layout_height="80dp"
    android:visibility="visible"
    app:layout_anchorGravity="center_horizontal|bottom"
    android:background="@drawable/logo"
    app:layout_anchor="@+id/bottom_appbar"
    android:layout_marginBottom="17dp"/>

<ImageButton
    android:id="@+id/fab_two"
    android:layout_width="190dp"
    android:layout_height="80dp"
    android:visibility="gone"
    app:layout_anchorGravity="center_horizontal|bottom"
    android:background="@drawable/logotwo"
    android:elevation="5dp"
    app:layout_anchor="@+id/bottom_appbar"
    android:layout_marginBottom="13dp">


</ImageButton>

你可以看到,我的主要活动中有框架布局,其中我处理所有的碎片。我使用图像按钮,而不是浮动的行动按钮,因为我想浮动的行动按钮的椭圆形shape.Now什么我想要的内部碎片,当用户滚动然后图像按钮,底部应用程序栏和视图,这是水平线隐藏?底部的应用程序栏被用在许多片段中,所以我需要一个代码,我可以写在一个活动中,隐藏底部应用程序栏和图像按钮,而用户在fragment.How中滚动,我可以做到这一点吗?我很抱歉我的愚蠢的问题,因为我是新的安卓开发.Thanks提前。

EN

回答 4

Stack Overflow用户

发布于 2019-03-18 14:56:55

您可以通过将以下两行放入xml来实现这一点

代码语言:javascript
复制
app:hideOnScroll="true"
app:layout_scrollFlags="scroll|enterAlways"

因此,完整的xml标记将是

代码语言:javascript
复制
<com.google.android.material.bottomappbar.BottomAppBar
    android:id="@+id/bottom_app_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    app:fabAlignmentMode="center"
    app:hideOnScroll="true"
    app:layout_scrollFlags="scroll|enterAlways"/>
票数 4
EN

Stack Overflow用户

发布于 2019-03-18 15:24:55

因为您在片段中滚动,所以需要将滚动值传递给您的活动。

我建议你使用Android Studio在Fragment的模板中生成的默认InteractionInterface

代码语言:javascript
复制
override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    val root = inflater.inflate(R.layout.fragment_blank, container, false)
    root.scrollView2.setOnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY ->
        // the key is here.
        var delta = scrollY - oldScrollY
        listener?.onFragmentScrolled(delta)
    }
    return inflater.inflate(R.layout.fragment_blank, container, false)
}


interface OnFragmentInteractionListener {
    // Name your function here
    fun onFragmentScrolled(delta: Float)
}

// the lines below are generated,
// not the key point here but important to binding listener
override fun onAttach(context: Context) {
    super.onAttach(context)
    if (context is OnFragmentInteractionListener) {
        listener = context
    } else {
        throw RuntimeException(context.toString() + " must implement OnFragmentInteractionListener")
    }
}

override fun onDetach() {
    super.onDetach()
    listener = null
}

然后,在YourActivity中实现YourFragment.OnFragmentInteractionListener

重写函数

代码语言:javascript
复制
override fun onFragmentScrolled(delta: Float) {
    anotherView.translationY = anotherView.translationY + delta
    if (anotherView.translationY > anotherView.height)
        anotherView.translationY = anotherView.height.toFloat()
    if (anotherView.translationY < 0)
        anotherView.translationY = 0f
}

结果将如下所示:mp4 link

要点是:将你的滚动动作从一个片段传递到另一个活动,你可以通过很多方式来实现这一点,这只是最基本的一种;

票数 1
EN

Stack Overflow用户

发布于 2019-08-06 08:02:37

与Wesely类似的方法如下所示。

让我们假设我们有这样的东西:

一个自定义的底部appbar,我们每个操作至少有一个片段。我的意思是说,activity main是所有片段都要放入的父对象。

您可以在main activity中声明一个接口(在本例中,我想隐藏/显示一个自定义的底部appbar和FAB),以执行底部appbar和fab的显示/隐藏(两者都在activity_main.xml中),因此接口将如下所示:

代码语言:javascript
复制
class MainActivity : AppCompatActivity(), OnScrollListenerMain {
...

    override fun fabAndBottomAppBarHide() {
        if (fab_main.isVisible) {
            fab_main.hide()
            bottom_app_bar.performHide()
        }
    }

    override fun fabAndBottomAppBarShow() {
        if (!fab_main.isVisible) {
            fab_main.show()
            bottom_app_bar.performShow()
        }
    }
...
} // end Main 

interface OnScrollListenerMain {
    fun fabAndBottomAppBarHide()
    fun fabAndBottomAppBarShow()
}

一旦在main activity中定义了接口,每个具有嵌套滚动视图的片段都可以实现它。

代码语言:javascript
复制
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/nsv_fragment_with_scroll"
    ... >

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

</androidx.core.widget.NestedScrollView>

创建对NestedScrollView的引用并附加onScrollChangeListener将帮助我们检查y轴上的滚动何时发生更改

代码语言:javascript
复制
class FragmentWithScroll : Fragment() {
lateinit var mScroll:NestedScrollView
...
override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        val view = inflater.inflate(R.layout.fragment_with_scroll,container,false)
        mScroll = view.findViewById(R.id.nsv_fragment_with_scroll)

        mScroll.setOnScrollChangeListener { v: NestedScrollView?, _: Int, scrollY: Int, _: Int, oldScrollY: Int ->
            val dy = oldScrollY - scrollY
            if (dy < 0) {
                (v!!.context as OnScrollListenerMain).fabAndBottomAppBarHide()
            } else if (dy > 0) {
                (v!!.context as OnScrollListenerMain).fabAndBottomAppBarShow()
            }
        }
        ...
        return view
    }
}

请记住,在Kotlin中,当lambda中的参数从未使用过时,我们可以使用_。所以基本上y的变化是val dy = oldScrollY - scrollYdy是负的,当滚动是从下到上的时,这个条件dy < 0为真,所以我们使用视图的上下文来调用OnScrollListenerMain.fabAndBottomAppBarHide()

同样,当滚动从上到下为时,dy为正,并且条件dy > 0为真,因此我们使用视图的上下文来调用OnScrollListenerMain.fabAndBottomAppBarShow()

但是,如果我们只想隐藏一个BottomAppbar (而不是隐藏FAB)呢?

BottomAppbar可能是协调器布局的子级,片段需要在其XML中有一个NestedScrollView、RecyclerView或ScrollView作为父元素,因此主活动XML应该类似于:

代码语言:javascript
复制
<androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/coordinator_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/lly_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" />

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab_main"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:src="@drawable/ic_add"
            app:borderWidth="0dp"
            app:layout_anchor="@+id/bottom_app_bar" />

        <com.google.android.material.bottomappbar.BottomAppBar
            android:id="@+id/bottom_app_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            app:fabAlignmentMode="center"
            app:hideOnScroll="true">
        </com.google.android.material.bottomappbar.BottomAppBar>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

这些片段应该放在lly_main中,下面这行代码就是诀窍:app:hideOnScroll="true"

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55215852

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档