由于工具栏Android,RelativeLayout内容的一部分从屏幕上消失



这里对Android来说非常新!我一直在做一个小项目,但我遇到了一个小问题。基本上,基本活动附带的Android工具栏正在计算到match-parent高度。让我再详细阐述一些。

在我的activity_main.xml中,我有以下一段代码:

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
... (fab)

然后在我的content_main.xml中,我有以下内容(片段):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/content"
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:background="@color/colorPrimary"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.michaeljones.testproject.MainActivity"
tools:showIn="@layout/activity_main">
<View
android:id="@+id/homeBackgroundTop"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerHorizontal="true"
android:background="@color/colorAccent" />
</RelativeLayout>

现在我的问题是,当我将视图的高度(以编程方式)设置为content_main.xml父级的高度(RelativeLayout)时,我的内容正在离开屏幕。我已将RelativeLayout设置为android:layout_height="match_parent",我希望将其填充屏幕的其余部分。然而,相反,它也考虑了工具栏。当我删除工具栏时,这一切都完美运行。

关于我如何使RelativeLayout中的android:layout_height="match_parent"不考虑 Android 工具栏,而是使其成为未使用的屏幕高度(全部减去工具栏)的任何想法?

编辑

我忘了提,以下是我如何抓住RelativeLayout的高度:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus == true) {
RelativeLayout content = (RelativeLayout) main.findViewById(R.id.content);
int w = content.getWidth();
int h = content.getHeight();
}
}

我认为将您所拥有的内容包装在RelativeLayout中应该可以解决您的问题。所以像这样的事情。

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
app:popupTheme="@style/AppTheme.PopupOverlay"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"/>
</android.support.design.widget.AppBarLayout>
<include
layout="@layout/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar_layout"/>
</RelativeLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/floating_dialpad_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:layout_margin="@dimen/connect_main_fab_margin"
android:background="@color/BackgroundActive"
android:src="@drawable/icon_keypad"/>
</android.support.design.widget.CoordinatorLayout>

最新更新