【问题标题】:Coordinator Layout View prevents other layouts from being displayed?协调器布局视图阻止显示其他布局?
【发布时间】:2015-12-07 01:20:00
【问题描述】:

我有一个CoordinatorLayout 用于显示AppBarLayout。但是,问题是我的RelativeLayoutLinearLayout 被我的ListView 隐藏了。此外,我的ListView 没有完全显示:相反,第一个ListView 元素的一部分被切断了,但我可以滚动查看其余部分。以下是我的尝试:

<?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"
    xmlns:fresco="http://schemas.android.com/tools"
    android:id="@+id/comments_coordinator_layout">

<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/comments_appbar"
        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_below="@id/comments_coordinator_layout"
        android:id="@+id/view_post"
        android:layout_width="match_parent"
        android:paddingRight="5dp"
        android:paddingLeft="5dp"
        android:orientation="horizontal"
        android:layout_height="175dp"
        android:background="#e6e6e6">

        <com.facebook.drawee.view.SimpleDraweeView
            android:layout_marginTop="15dp"
            android:id="@+id/poster_picture"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_marginLeft="10dp"
            fresco:placeholderImage="@mipmap/blank_prof_pic"
            fresco:roundedCornerRadius="5dp"
            />

        <TextView
            android:layout_marginLeft="5dp"
            android:layout_marginTop="15dp"
            android:layout_toRightOf="@id/poster_picture"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15sp"
            android:textStyle="bold"
            android:id="@+id/poster_name"/>

        <TextView
            android:layout_alignParentRight="true"
            android:layout_marginTop="15dp"
            android:layout_toRightOf="@id/poster_name"
            android:layout_marginLeft="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15sp"
            android:id="@+id/post_date"/>

        <TextView
            android:layout_marginLeft="5dp"
            android:layout_toRightOf="@id/poster_picture"
            android:layout_below="@id/poster_name"
            android:textSize="20sp"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/view_status" />

    </RelativeLayout>

            <ListView
                android:id="@+id/lv_comments_feed"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_above="@+id/send_message"
                android:layout_below="@id/view_post">
            </ListView>

        <LinearLayout
            android:id="@+id/send_message"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:layout_alignParentBottom="true"
            android:orientation="horizontal" >

            <EditText
                android:id="@+id/write_comment"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:layout_weight="5"
                android:gravity="top|left"
                android:hint="Comment back!"
                android:inputType="textMultiLine"
                android:background="@color/white"
                android:scrollHorizontally="false" />

            <Button
                android:id="@+id/send_comment"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:gravity="center"
                android:text="send"
                android:textAppearance="?android:attr/textAppearanceMedium" />
        </LinearLayout>

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

我不明白的一点是,为什么当我单击布局的design 选项卡时,我的所有布局似乎都相互重叠,而不是显示在彼此下方。此外,我不明白为什么ListView 优先于我的LinearLayoutRelativeLayout 显示。有什么办法可以让我从上到下具有以下结构:AppBarLayout -> RelativeLayout -> ListView -> LinearLayout。任何帮助,将不胜感激。谢谢!

如果这意味着什么,在我的 AndroidManifest 中,我有 android:windowSoftInputMode="stateVisible"

块引用

编辑:这是我想要实现的一个例子

编辑:在听取了小太阳的一些建议后,这是我目前所得到的:

<?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"
    xmlns:fresco="http://schemas.android.com/tools"
    android:id="@+id/comments_coordinator_layout">

<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/comments_appbar"
        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_marginTop="?attr/actionBarSize"
        android:layout_below="@id/comments_coordinator_layout"
        android:id="@+id/view_post"
        android:layout_width="match_parent"
        android:paddingRight="5dp"
        android:paddingLeft="5dp"
        android:orientation="horizontal"
        android:layout_height="175dp"
        android:background="#e6e6e6">

        <com.facebook.drawee.view.SimpleDraweeView
            android:layout_marginTop="15dp"
            android:id="@+id/poster_picture"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_marginLeft="10dp"
            fresco:placeholderImage="@mipmap/blank_prof_pic"
            fresco:roundedCornerRadius="5dp"
            />

        <TextView
            android:layout_marginLeft="5dp"
            android:layout_marginTop="15dp"
            android:layout_toRightOf="@id/poster_picture"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15sp"
            android:textStyle="bold"
            android:id="@+id/poster_name"/>

        <TextView
            android:layout_alignParentRight="true"
            android:layout_marginTop="15dp"
            android:layout_toRightOf="@id/poster_name"
            android:layout_marginLeft="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15sp"
            android:id="@+id/post_date"/>

        <TextView
            android:layout_marginLeft="5dp"
            android:layout_toRightOf="@id/poster_picture"
            android:layout_below="@id/poster_name"
            android:textSize="20sp"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/view_status" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:layout_marginTop="225dp"
            android:id="@+id/lv_comments_feed"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/send_message">
        </ListView>


        <LinearLayout
            android:id="@+id/send_message"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:layout_alignParentBottom="true"
            android:orientation="horizontal" >

            <EditText
                android:id="@+id/write_comment"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:layout_weight="5"
                android:gravity="top|left"
                android:hint="Comment back!"
                android:inputType="textMultiLine"
                android:scrollHorizontally="false" />

            <Button
                android:id="@+id/send_comment"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:gravity="center"
                android:text="send"
                android:textAppearance="?android:attr/textAppearanceMedium" />
        </LinearLayout>

    </RelativeLayout>


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

编辑:EditText 的问题。如您所见,当我单击EditText 时,布局仅显示ListView,并隐藏包括ActionToolBarStatus Bar 在内的所有内容。

编辑:显示 Android 清单

    <activity android:name=".com.tabs.activity.Comments"
        android:label="View Post"
        android:theme="@style/AppTheme.NoActionBar"
        android:windowSoftInputMode="adjustPan">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".com.tabs.activity.news_feed"/>
    </activity>

编辑:Tiny Sunlight 的建议:

<FrameLayout>
  <ToolBar></ToolBar>
  <Relative Layout as List header></RelativeLayout as ListHeader>
  <ListView></ListView>
  <LinearLayout for Edit Text></LinearLayout for EditText>
</FrameLayout>

【问题讨论】:

  • 请上传你想要实现的图片。
  • 添加了我想要的图片!
  • 只需添加 marginTop = RelativeLayout's height 和 marginBotton = LinearLayout' height 和 ListView's height 应该设置为 match_parent 。如果您不需要动画,请删除 CoordinatorLayout AppBarLayout 并将所有视图包含在 RelativeLayout 中。您是否需要像向上滚动 Listview 时那样的动画,RelativeLayout'height 变为零?
  • @tinysunlight 我不需要动画,但我只是想知道,如何在没有协调器布局的情况下实现我在上图中发布的工具栏外观?我认为这是我能做到的唯一方法。
  • 我添加了一个答案。 Toolbar 只是一个 View 。它与 Coordinator Layout 无关。

标签: android android-layout listview android-listview android-coordinatorlayout


【解决方案1】:

只需添加一个 ScrollView 并将 softInputMode 设置为 adjustResize。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    android:fitsSystemWindows="true"
    android:layout_width="match_parent" android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <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:fitsSystemWindows="true"
        xmlns:fresco="http://schemas.android.com/tools"
        android:id="@+id/comments_coordinator_layout">

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




        <RelativeLayout
            android:layout_below="@id/comments_appbar"
            android:id="@+id/view_post"
            android:layout_width="match_parent"
            android:paddingRight="5dp"
            android:paddingLeft="5dp"
            android:orientation="horizontal"
            android:layout_height="175dp"
            android:background="#e6e6e6">

            <com.facebook.drawee.view.SimpleDraweeView
                android:layout_marginTop="15dp"
                android:id="@+id/poster_picture"
                android:layout_width="75dp"
                android:layout_height="75dp"
                android:layout_marginLeft="10dp"
                fresco:placeholderImage="@mipmap/blank_prof_pic"
                fresco:roundedCornerRadius="5dp"
                />

            <TextView
                android:layout_marginLeft="5dp"
                android:layout_marginTop="15dp"
                android:layout_toRightOf="@id/poster_picture"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="15sp"
                android:textStyle="bold"
                android:id="@+id/poster_name"/>

            <TextView
                android:layout_alignParentRight="true"
                android:layout_marginTop="15dp"
                android:layout_toRightOf="@id/poster_name"
                android:layout_marginLeft="5dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="15sp"
                android:id="@+id/post_date"/>

            <TextView
                android:layout_marginLeft="5dp"
                android:layout_toRightOf="@id/poster_picture"
                android:layout_below="@id/poster_name"
                android:textSize="20sp"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/view_status" />

        </RelativeLayout>

        <ListView
            android:id="@+id/lv_comments_feed"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/send_message"
            android:layout_below="@id/view_post">
        </ListView>

        <LinearLayout
            android:id="@+id/send_message"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:layout_alignParentBottom="true"
            android:orientation="horizontal" >

            <EditText
                android:id="@+id/write_comment"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:layout_weight="5"
                android:gravity="top|left"
                android:hint="Comment back!"
                android:inputType="textMultiLine"
                android:background="@color/white"
                android:scrollHorizontally="false" />

            <Button
                android:id="@+id/send_comment"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:gravity="center"
                android:text="send"
                android:textAppearance="?android:attr/textAppearanceMedium" />
        </LinearLayout>

    </RelativeLayout>
</ScrollView>

【讨论】:

  • 如果我将 Coordinator Layout 替换为 RelativeLayout 我的 Android 状态栏全是白色的,但其他一切似乎都正常。你知道如何解决这个问题吗?
  • 只有当我使用 Coordinator Layout 时,我才能看到我的状态栏颜色正确,但当我使用 RelativeLayout 时,我看不到。我想知道为什么会这样。谢谢你帮助我。
  • 我实际上已经足够有趣了,但是现在每当我点击我的EditText 时,您唯一可以明显看到的部分是ListView,但您看不到RelativeLayout id 为view_post。我已经在上面发布了我的代码,以显示我所做的更改。在我的 AndroidManifest.xml 我有 android:windowSoftInputMode="adjustPan" 。如果你能帮助我,那就太好了。谢谢!
  • 请更新软键盘显示和AndroidManifest时的截图。
  • 您好,我更新了截图。关于AndroidManifest,您希望看到什么?我认为这不会有太大帮助。我在上面的帖子中包含了activity 标签。
猜你喜欢
  • 2018-06-29
  • 2019-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-21
  • 2019-11-06
  • 2015-12-09
  • 1970-01-01
相关资源
最近更新 更多