安卓 - 相对布局高度填充可折叠工具栏布局的剩余空间

12

我有以下设计,顶部有CollapsingToolbarLayout,下面有RelativeLayout:

enter image description here

我想让RelativeLayout填充剩余空间直到底部,以便我可以像这样居中内容:

enter image description here

我尝试在RelativeLayout上使用match_parent,但它占用了整个根布局的大小,包括CollapsingToolbarLayout:

enter image description here

我该怎么办?这是完整布局的代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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"
    tools:context=".UserActivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorWhite">

    <android.support.design.widget.AppBarLayout android:id="@+id/activity_user_appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/activity_user_collapsingtoolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <android.support.v7.widget.Toolbar android:id="@+id/activity_user_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:contentInsetLeft="0dp"
                app:contentInsetStart="0dp"
                app:popupTheme="@style/AppTheme.PopupOverlay"
                app:layout_collapseMode="pin">

                <!-- toolbar content -->

            </android.support.v7.widget.Toolbar>

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

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout android:id="@+id/activity_user_eventsalert"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/calendar_remove_titlegray_48px"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:text="No events yet"
                android:textSize="20sp"
                android:textColor="@color/colorTitleGray"/>
        </LinearLayout>

    </RelativeLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="10dp"
        android:layout_gravity="bottom"
        android:background="@drawable/shadow_bottom_white_to_transparent"/>

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

1
你是否尝试过运行应用程序或仅使用预览?因为假定你的字符串资源已正确设置RelativeLayout的行为,代码看起来是正确的。 - darnmason
哪一个是正确的?match_parent 还是 _wrap_content_?@darnmason - Alex Ardavin
匹配父级... - darnmason
你能详细说明你的问题吗?看起来你的代码没问题。 - Shaishav Jogani
你是否在使用Fragment来填充这个视图?如果是这样,你能否检查一下托管该Fragment的宿主Activity的高度属性是多少? - Wubbalubbadubdub
4个回答

5
尝试使用应用程序栏滚动监听器
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;

    mAppBar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {

                  int appbarHeight=appBarLayout.getHeight(); // or measured height
                  int totalHeightOfDevice=height;
                  int absoluteHeight=height-appbarHeight;

                  // position your view to absolute height..
            }
        });

我觉得使用这个方法很不好...但这似乎是解决CoordinatorLayout的怪异问题的唯一方法。对我来说有效,谢谢! - EboMike

0

请参考以下代码:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:id="@+id/activity_user_eventsalert"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:orientation="vertical">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="No events yet"
            android:textColor=""
            android:textSize="20sp"/>
    </LinearLayout>

</RelativeLayout>

那并没有帮助。问题在于RelativeLayout因为match_parent而太大了。 - EboMike

-1
<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_weight="1"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/calendar_remove_titlegray_48px"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:text="No events yet"
                android:textSize="20sp"
                android:textColor="@color/colorTitleGray"/>
        </LinearLayout>

    </RelativeLayout>

CoordinatorLayout.LayoutParams 没有 layout_weight - EboMike

-1

将此代码添加到相对布局中:

android:gravity="center"
android:layout_weight="1"

CoordinatorLayout.LayoutParams 没有 layout_weight - EboMike

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接