【问题标题】:How can I adjust the weight of frame layout?如何调整框架布局的权重?
【发布时间】:2023-08-28 17:24:02
【问题描述】:

所以我正在尝试调整框架布局的权重,以便它可以覆盖整个活动页面。 Result of what happens.

这是我当前活动的 xml。

<?xml version="1.0" encoding="utf-8"?>
<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">

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

    <TextView
        android:id="@+id/textView13"
        android:layout_width="226dp"
        android:layout_height="48dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_marginEnd="68dp"
        android:layout_marginTop="26dp"
        android:fontFamily="@font/passion_one"
        android:gravity="center"
        android:text="Kung Fu Tea Order"
        android:textColor="#000000"
        android:textSize="30sp"
        />

    <ListView
        android:layout_width="match_parent"
        android:layout_height="393dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="90dp" />


    <Button
        android:id="@+id/addAnotherDrink"
        android:layout_width="296dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="100dp"
        android:background="@drawable/btn_shape"
        android:fontFamily="@font/passion_one"
        android:text="ADD ANOTHER DRINK"
        android:textColor="#ffffff"
        android:textSize="20sp" />

    <Button
        android:id="@+id/placeOrderBtn"
        android:layout_width="296dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="36dp"
        android:background="@drawable/btn_shape"
        android:fontFamily="@font/passion_one"
        android:text="PLACE ORDER"
        android:textColor="#ffffff"
        android:textSize="20sp" />

    <ImageView
        android:id="@+id/imageView8"
        android:layout_width="83dp"
        android:layout_height="71dp"
        android:layout_above="@+id/_dynamic"
        android:layout_marginBottom="-90dp"
        android:layout_toStartOf="@+id/textView13"
        app:srcCompat="@drawable/logo"
        tools:ignore="UnknownId" />


</RelativeLayout>

在我的 java 类中,我基本上是使用一个按钮来导航到一个片段,方法是使用框架布局作为容器。

真的,有没有办法改变这个框架布局的权重,这样我之前的活动就不会显示了?

【问题讨论】:

  • FragmentTransactions 仅处理 Fragments' Views。您应该将原始内容(即&lt;TextView&gt;&lt;ListView&gt; 等)放入您在启动时加载的自己的Fragment,并且可以根据需要将replace() 与您的其他Fragments 一起使用。跨度>
  • 你是说制作另一个容器来放置我的片段,以便它可以替换我的其他片段?
  • 我的意思是,如果你想用你的Fragment替换Activity的整个布局,那么你的Activity的布局应该只是容器FrameLayout,你现在拥有的所有东西都应该在它自己的Fragment 中。您将在启动时将 Fragment 加载到 onCreate() 中。然后,当您想切换时,使用replace() 事务处理新的Fragment

标签: android android-layout android-layout-weight


【解决方案1】:

您可以将整个活动的基础设为框架布局。然后您可以将布局相互叠加,因此 FrameLayoutBase > RelativeLayout from above > FrameLayout 您想要覆盖整个屏幕。然后只需根据需要调整布局的可见性。注意:Relative Layout 和 Overlay FrameLayout 都是 FrameLayoutBase 的独立子级。

【讨论】:

    最近更新 更多