android - RelativeLayout 未使用协调器布局 android 正确绘制

标签 android android-layout android-fragments coordinator-layout

我编写了一个简单的 fragment ,其中有几个 TextView 和一个按钮,都在一个 RelativeLayout 中。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context=".fragments.TutorialFragment">

    <TextView
        android:id="@+id/tuto_title"
        style="?android:textAppearanceLarge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:padding="20dp"
        android:text="@string/tutorial_title"/>

    <TextView
        android:id="@+id/tuto_msg"
        style="?android:textAppearanceLarge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tuto_title"
        android:padding="20dp"
        android:text="@string/tutorial_txt"/>

    <Button
        android:id="@+id/nextBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_margin="15dp"
        android:text="@string/next"/>

</RelativeLayout>

此 fragment 在 StudioDesigner 上显示效果很好,但是在执行代码时标题丢失了 AVD Running

Activity 布局是

<?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"
android:id="@+id/coordinatorLayout"
tools:context="com.crocodil.software.a1ccalc.A1CActivity">

<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>


<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/fragment_container"
             android:layout_width="match_parent"
             android:layout_height="match_parent" />

看起来 fragment 在放置工具栏之前计算其可用空间...

这是我在设计器中看到的屏幕

Designer

感谢任何提示

最佳答案

问题是 FrameLayout 在工具栏后面,在具有工具栏的协调器布局中添加 View 的主要方法是设置 margin 或添加 app:layout_behavior ="@string/appbar_scrolling_view_behavior" 到框架布局

<?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"
android:id="@+id/coordinatorLayout"
tools:context="com.crocodil.software.a1ccalc.A1CActivity">

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/fragment_container"
             android:layout_width="match_parent"
             android:layout_marginTop="?attr/actionBarSize"
             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>

关于android - RelativeLayout 未使用协调器布局 android 正确绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35942757/

相关文章:

java - 在 RecyclerView 中更改单个可绘制对象的颜色将更改所有可绘制对象

返回 Activity 后 Android 服务挂起

android - 使用 adb 刷新 Android 媒体存储

android - ActivityManager#getRunningServices 返回手动停止的服务

java - 如何使用文件中的值绘制折线图?

android - 在没有任何代码的情况下在 xml 文件中定义 Tabhost/TabWidget + 内容 - 这可能吗?

android - 如何在android中制作一个圆形搜索栏,其图像适合其圆圈

android xml布局,如何将内容填充到整个屏幕,保持页脚完好无损?

java - 为什么我使用 bundle 在 fragment 中获取空值

android - onTabSelected 改变 Fragment