查看覆盖操作工具栏?
View covering action toolbar?
我正在尝试在我的屏幕上创建一个与 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
覆盖了我的工具栏。这是一张显示发生了什么的图片:
我真的不明白为什么会这样,是否可以对齐我的 EditText
使其不覆盖工具栏?谢谢!
UPDATE: 将Arya的代码添加到我的代码后,我的图像看起来像这样,这使得ActionBar
太高了。关于如何修复它有什么想法吗?
<android.support.design.widget.AppBarLayout
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:theme="@style/AppTheme.AppBarOverlay">
<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>
您的布局在其当前定义中没有多大意义。我认为这里的问题是 CoordinatorLayout
已被分配 fitsSystemWindows=true
。这使得它从状态栏的最顶部报告其 bottom
。另一方面,RelativeLayout
期望状态栏底部的 bottom
值。这会使您的 EditText
与 Toolbar
重叠 25dp
- 状态栏的标准高度。您可以通过将 android:fitsSystemWindows="true"
分配给 ID 为 create_post
的 RelativeLayout
来确认这一点。在这种情况下,CoordinatorLayout
的 bottom
将被正确报告,而 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
放在 RelativeLayout
中的特定原因,请分享。
我正在尝试在我的屏幕上创建一个与 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
覆盖了我的工具栏。这是一张显示发生了什么的图片:
我真的不明白为什么会这样,是否可以对齐我的 EditText
使其不覆盖工具栏?谢谢!
UPDATE: 将Arya的代码添加到我的代码后,我的图像看起来像这样,这使得ActionBar
太高了。关于如何修复它有什么想法吗?
<android.support.design.widget.AppBarLayout
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:theme="@style/AppTheme.AppBarOverlay">
<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>
您的布局在其当前定义中没有多大意义。我认为这里的问题是 CoordinatorLayout
已被分配 fitsSystemWindows=true
。这使得它从状态栏的最顶部报告其 bottom
。另一方面,RelativeLayout
期望状态栏底部的 bottom
值。这会使您的 EditText
与 Toolbar
重叠 25dp
- 状态栏的标准高度。您可以通过将 android:fitsSystemWindows="true"
分配给 ID 为 create_post
的 RelativeLayout
来确认这一点。在这种情况下,CoordinatorLayout
的 bottom
将被正确报告,而 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
放在 RelativeLayout
中的特定原因,请分享。