查看覆盖操作工具栏?

时间:2015-11-30 03:42:15

标签: android android-layout android-toolbar android-relativelayout android-coordinatorlayout

我正在尝试在我的scren上创建与EditText相关联的工具栏。以下是我尝试创建的xml文件的内容:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tool="http://schemas.android.com/tools"
    tool:context=".CreatePost"
    android:id="@+id/create_post">

    <include layout="@layout/app_bar" android:layout_width="match_parent" android:layout_height="wrap_content"
        android:id="@+id/create_post_toolbar"/>

    <!-- Edit text for typing status. Light gray placeholder. -->

    <EditText
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColorHint="#d3d3d3"
        android:layout_below="@id/create_post_toolbar"
        android:hint="Update your friends!"
        android:padding="10dp"
        android:gravity="top"
        android:background="@android:color/transparent"
        android:inputType="textMultiLine"
        android:id="@+id/type_status"/>

</RelativeLayout>

app_bar.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=".CreatePost">

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

        <android.support.v7.widget.Toolbar android:id="@+id/toolbar_universal"
            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />

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

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

然而,当我这样做时,我的EditText覆盖了我的工具栏。这是一张显示发生情况的图片:

Image showing screen

我真的不明白为什么会发生这种情况,无论如何都要调整我的EditText所以它不会覆盖工具栏?谢谢!

更新:将Arya的代码添加到我的代码后,我的图像看起来像是这样,这使得ActionBar太高了。关于如何解决它的任何想法?

Action bar too high

3 个答案:

答案 0 :(得分:2)

您的布局在其当前定义中没有多大意义。我认为这里的问题是CoordinatorLayout已被分配fitsSystemWindows=true。这使得它从状态栏的最顶部报告bottom。另一方面,RelativeLayout期望状态栏底部的bottom值。这会使EditTextToolbar重叠25dp - 状态栏的标准高度。您可以通过将android:fitsSystemWindows="true"分配给ID为RelativeLayout的{​​{1}}来确认这一点。在这种情况下,create_post bottom将被正确报告,而CoordinatorLayout 不会与EditText重叠。

通常,Toolbar设置为父级,内容(在您的情况下为CoordinatorLayout)位于RelativeLayout的子级中:

CoordinatorLayout

如果您有将<?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=".CreatePost"> <android.support.design.widget.AppBarLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar_universal" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/create_post"> <EditText android:layout_width="match_parent" android:layout_height="match_parent" android:textColorHint="#d3d3d3" android:hint="Update your friends!" android:padding="10dp" android:gravity="top" android:background="@android:color/transparent" android:inputType="textMultiLine" android:id="@+id/type_status"/> </RelativeLayout> </android.support.design.widget.CoordinatorLayout> 置于CoordinatorLayout内的具体原因,请分享。

答案 1 :(得分:1)

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/htab_maincontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/tabheader"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
    android:id="@+id/htab_appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 
        <android.support.v7.widget.Toolbar
            android:id="@+id/htab_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:minHeight="?attr/actionBarSize"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>         
</android.support.design.widget.AppBarLayout>

答案 2 :(得分:0)

 <android.support.design.widget.AppBarLayout 
    android:layout_height="?attr/actionBarSize"
    android:layout_width="match_parent" 
    android:theme="@style/AppTheme.AppBarOverlay">