如何在FrameLayout中将元素放在彼此之下?

问题描述 投票:-1回答:1

我在我的布局中使用FrameLayout,并在第一个LinearLayout上显示按钮。我想在第一个LinearLayout下显示此按钮,而不是将FrameLayout更改为LinearLayoutRelativeLayout。可能吗?没有找到任何解决方案,只是将FrameLayout改为LinearLayoutRelativeLayout

这就是它现在的样子:Screenshot

这是我的xml代码:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    android:id="@id/menu_container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    style="@style/LeftDrawer"
    xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
    android:id="@id/menu_user_profile"
    android:clickable="true"
    android:focusable="true"
    style="@style/MenuTopBox">
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="@dimen/activity_margin_horizontal"
        android:layout_weight="1.0">
        <ImageView
            android:id="@id/menu_avatar"
            android:background="@color/transparent"
            android:layout_width="@dimen/avatar_default_size"
            android:layout_height="@dimen/avatar_default_size"
            android:src="@drawable/ic_default_avatar"
            android:scaleType="fitCenter" />
        <ImageView
            android:id="@id/menu_avatar_photo"
            android:background="@color/transparent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_logo_camera"
            android:scaleType="fitCenter"
            android:layout_alignBottom="@id/menu_avatar"
            android:layout_alignParentRight="true" />
    </RelativeLayout>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="0.5">
        <TextView
            android:gravity="center_vertical"
            android:id="@id/menu_profile"
            android:layout_height="fill_parent"
            android:layout_marginLeft="@dimen/activity_margin_horizontal"
            android:layout_marginRight="@dimen/margin_micro"
            android:text="@string/menu_profile"
            android:textColor="@color/white"
            style="@style/TextBig" />
    </LinearLayout>
</LinearLayout>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <Button
        android:id="@id/menu_logout_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/menu_logout_button"
        android:drawableLeft="@drawable/ic_menu_logout"
        style="@style/ButtonMenu" />
</LinearLayout>
</FrameLayout>
android android-layout android-framelayout
1个回答
0
投票

您的布局包含许多嵌套视图,这可能会导致布局变慢并执行比其需要更多的计算。

我可以推荐使用ConstraintLayout

但是为什么 - 这是一个简单的布局,旨在优化和展平布局的视图层次结构(意味着没有嵌套视图),从而加快布局的加载时间。

此外,constraintLayout将为您节省大量时间,因为使用它进行布局非常简单。

以下是您希望使用constraintLayout的布局示例:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">


<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="Button"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/textView8"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:text="text"
    android:textSize="18sp"
    app:layout_constraintBottom_toBottomOf="@+id/button3"
    app:layout_constraintStart_toEndOf="@+id/button3"
    app:layout_constraintTop_toTopOf="@+id/button3" />

<Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:text="Button"
    app:layout_constraintEnd_toEndOf="@+id/button3"
    app:layout_constraintStart_toStartOf="@+id/button3"
    app:layout_constraintTop_toBottomOf="@+id/button3" />

<Button
    android:id="@+id/button5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:text="camera"
    app:layout_constraintBottom_toBottomOf="@+id/button4"
    app:layout_constraintStart_toEndOf="@+id/button4"
    app:layout_constraintTop_toTopOf="@+id/button4" />
</androidx.constraintlayout.widget.ConstraintLayout>
© www.soinside.com 2019 - 2024. All rights reserved.