【问题标题】:Android Button vs TextView visibility orderAndroid Button vs TextView 可见性顺序
【发布时间】:2016-11-13 21:44:40
【问题描述】:

我有一种情况,即使在我将片段添加到片段容器之后,属于 Activity 的按钮始终位于顶部(可见),但是一旦我将元素类型更改为 TextView,它就会像应有的那样停留在新片段的后面。

作为 TextView 的元素 - 停留在新添加的片段后面

<TextView
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:id="@+id/openAdFilteringButton"
        android:text="Filters"
        android:onClick="openAdFiltering"
        android:layout_marginBottom="80dp"
        android:layout_gravity="center|bottom"/>

作为 Button 的元素 - 位于新添加的片段之上(保持可见)

<Button
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:id="@+id/openAdFilteringButton"
        android:text="Filters"
        android:onClick="openAdFiltering"
        android:layout_marginBottom="80dp"
        android:layout_gravity="center|bottom"/>

该元素是 FrameLayout 的子元素。

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

请注意,此 FrameLayout 是片段容器,这意味着我正在将片段添加到此 FrameLayout:

FragmentTransaction.add(R.id.fragment_container, adFilteringFragment, "AdFilteringFragment").addToBackStack("AreaInterFrag").commit();

我需要告诉按钮什么,以便他像 TextView 一样工作并留在片段后面。这种行为差异(Button vs TextView)来自哪里? 谢谢!

【问题讨论】:

    标签: android android-layout android-fragments


    【解决方案1】:

    我想你正试图通过重叠一些东西来隐藏按钮。 要实现这种行为并以编程方式隐藏视图,您最好调用

    mView.setVisibility(View.INVISIBLE);
    

    关于按钮为何如此表现的原因,请尝试阅读此内容,看看是否可以帮助您:Button always displays on top in FrameLayout

    【讨论】:

    • 当您不需要这样做时,设置按钮的可见性并不是一个好的解决方案。有多种情况可以处理取回按钮(回栈更改、片段杀死等)。您的链接回答了问题,解决方案是将其添加到按钮:android:stateListAnimator="@null"
    • 我很高兴有帮助
    猜你喜欢
    • 1970-01-01
    • 2020-12-06
    • 2015-07-14
    • 2017-11-15
    • 2023-03-23
    • 1970-01-01
    • 2013-06-14
    • 2016-07-02
    • 2012-04-21
    相关资源
    最近更新 更多