【问题标题】:Framelayout not appearing top of RecyclerView in constraintlayoutFramelayout 没有出现在约束布局中 RecyclerView 的顶部
【发布时间】:2018-03-01 14:25:12
【问题描述】:

我是这个 android 编程的新手。

我试图在回收站视图(它是 ConstraintLayout 的一部分,我需要将 FAB 放置在屏幕右下角)之上有一个框架布局和一个 textview,但它似乎没有出现.

感谢您就为什么会发生这种情况以及应该采取的措施提供一些帮助和建议。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:background="#E0E0E0"
    tools:context="com.testapp.testapp.HomeActivity">
    <FrameLayout
        android:id="@+id/org_store_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:paddingBottom="12dp"
        android:paddingLeft="12dp"
        android:paddingRight="12dp">

            <TextView
                android:id="@+id/order_ID"
                style="@style/AppTheme.Subheader"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="0dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="-2dp"
                android:ellipsize="end"
                android:maxLines="1"
                android:visibility="visible"
                tools:text="Order #: 0001123" />
    </FrameLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_orders"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        tools:listitem="@layout/item_orders"

        />

    <View
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:layout_below="@+id/recycler_orders"
        android:background="@drawable/bg_shadow" />

    <!-- Empty list (pizza guy) view ... havent tested empty view-->
    <android.support.constraint.ConstraintLayout
        android:id="@+id/view_empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:orientation="vertical"
        android:visibility="gone"
        tools:visibility="gone">

        <ImageView
            style="@style/AppTheme.PizzaGuy"
            android:contentDescription="@string/message_no_results"
            android:src="@drawable/pizza_monster" />

        <TextView
            style="@style/AppTheme.Body1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/message_no_results"
            android:textColor="@color/grey_300" />
    </android.support.constraint.ConstraintLayout>

    <ProgressBar
        android:id="@+id/progress_loading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/recycler_orders"
        android:layout_alignTop="@+id/recycler_orders"
        android:layout_centerHorizontal="true"
        android:visibility="gone" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_addnew"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:backgroundTint="@color/colorAccent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:srcCompat="@drawable/ic_add_white_24px" />

</android.support.constraint.ConstraintLayout>

【问题讨论】:

标签: android android-recyclerview android-framelayout


【解决方案1】:

ConstraintLayout 的好处是它非常强大,您不再需要使用嵌套布局。因此,在您的情况下,不需要嵌套的 FrameLayout 和嵌套的 ConstraintLayout。

例如,您可以直接在 order_ID TextView 上指定背景和填充,并且可以摆脱 FrameLayout。此外,view_empty 中的所有内容都可以直接作为主 ContraintLayout 的子项。您的布局中缺少的是约束! ConstraintLayout 需要约束来知道在哪里绘制它的孩子,但你还没有提供任何!这意味着,由于布局不知道在哪里绘制东西,所以一切都将在布局本身的左上角。

最后一件事:ConstraintLayout 与 ltr 和 rtl 语言完美配合。因此,最好使用marginStart 和marginEnd,而不是marginLeft 和marginRight。

【讨论】:

猜你喜欢
  • 2021-10-07
  • 1970-01-01
  • 2023-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多