【问题标题】:layout behavior not working in frame layout布局行为在框架布局中不起作用
【发布时间】:2019-05-19 11:14:52
【问题描述】:

我试图让我的工具栏在内容滚动时隐藏/显示,当我将它注入 FrameLayout 容器并且它不起作用时,这是我的 appbar 布局:

<android.support.design.widget.CoordinatorLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".BaseActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/tool_bar_bg"
        app:elevation="0dp"
        android:theme="@style/AppTheme.AppBarOverlay"
        android:fitsSystemWindows="true"
        android:id="@+id/app_bar_layout">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_marginStart="10dp"
            android:layout_marginEnd="10dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            app:layout_scrollFlags="scroll|enterAlways"
            android:background="@drawable/bg_actionbar"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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



        <!-- My scrolling content -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>



    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email" />

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

和我的 SecondActivity 布局

   <android.support.v4.widget.NestedScrollView xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:fillViewport="true"
        android:gravity="center"
        android:paddingLeft="20dp"
        android:paddingRight="20dp">

        <TextView
            android:id="@+id/textone"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@string/content"
            android:textSize="25dp" />


    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

这是我用来扩展活动布局的 java 代码:

    public class FirstActivity extends BaseActivity {   
       @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            //inflate your activity layout here!
            @SuppressLint("InflateParams")
            View contentView = inflater.inflate(R.layout.activity_user, null, false);
            drawer.addView(contentView, 0);
}

当我滚动它时它不起作用,并且我也使用RelativeLayout,LinearLayout 签入了 SecondActivity,但它没有隐藏。

【问题讨论】:

标签: android android-layout


【解决方案1】:

布局充气机的问题我已经改变了我的布局充气机:

FrameLayout container = (FrameLayout) findViewById(R.id.content_frame);
        View inflatedLayout= getLayoutInflater().inflate(R.layout.activity_user, null, false);
        container.addView(inflatedLayout);

【讨论】:

    【解决方案2】:

    请使用android.support.design.widget.CollapsingToolbarLayout包围您想要在滚动过程中隐藏/显示的项目

    在此处阅读有关该主题的更多信息, collapsing-toolbar-layout

    【讨论】:

      猜你喜欢
      • 2014-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多