[英]Android: toolbar pushes relative layout down, beyond screen boundary
我看到了類似問題的問題,但是當我嘗試實施他們的回答時,卻無法解決我的問題。
如您所見,底部的“棍子”超出了屏幕邊界。
這是我的兩個xml文件。
Activity.xml :
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".Activities.ScreenActivity">
<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_screen" />
</android.support.design.widget.CoordinatorLayout>
content_screen.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/design_navigation_elevation"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".Activities.ScreenActivity"
tools:showIn="@layout/activity_screen"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_gravity="center|top"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="That's it! Press the button to use your gadget!"
android:id="@+id/textView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:layout_alignParentLeft="false"
android:layout_alignParentTop="true"
android:layout_alignParentStart="false" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="\u2714"
android:id="@+id/closeAppButton"
android:layout_gravity="center"
android:background="@drawable/button_bg_round"
android:longClickable="false"
android:textSize="64dp"
android:layout_alignParentLeft="false"
android:layout_alignParentTop="false"
android:layout_centerInParent="true"
android:textColor="#06BF60"
android:textStyle="bold" />
</RelativeLayout>
這是因為app:layout_behavior="@string/appbar_scrolling_view_behavior"
屬性(如果您要刪除此屬性),則布局會app:layout_behavior="@string/appbar_scrolling_view_behavior"
在工具欄上。
為了避免這種情況,您需要將靜態高度設置為相對布局或wrapcontent!
我認為問題在於activity_main.xml中的協調器布局有2個子級-AppBarLayout和包含的content_screen.xml。 AppBarLayout通常將其layout_height屬性設置為wrap_content,但問題是content_screen.xml的layout_height屬性設置為match_parent 。 現在,由於它是activity_main的子代,因此content_screen從activity_main獲取其高度,從而導致其溢出。
解決的辦法是在activity_main.xml中創建CoordinatorLayout的孩子,在這個類似的答案提出的包裹AppbarLayout和content_screen在它這里 。
也許有更好的方法,但是我認為這應該可以解決您的問題:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".Activities.ScreenActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<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_screen" />
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.