【问题标题】:Fragment layout help needed需要片段布局帮助
【发布时间】:2020-11-23 05:46:39
【问题描述】:

这可能很简单,但我似乎无法弄清楚我做错了什么。

我正在编写一个 android 应用程序 (java),并且我正在从一个 api 调用的片段中填充一个列表。

起初片段显示全屏而不是作为活动的一部分,所以我将列表放入布局中(如下所示)

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".ProvinceFragment">

<!-- TODO: Update blank fragment layout -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_margin="10dp"
        android:text="Heading"
        android:textSize="30sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/menuHomeButton"
        android:layout_height="30dp"
        android:layout_width="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:textSize="20sp"
        android:text="First bit"
        />


    <TextView
        android:id="@+id/menuChangeProvButton"
        android:layout_height="30dp"
        android:layout_width="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:textSize="20sp"
        android:text="Second bit"
        />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/listLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ListView
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ProgressBar
            android:id="@+id/progressBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:visibility="gone" />

    </androidx.constraintlayout.widget.ConstraintLayout>


</LinearLayout>

这会将列表呈现为原始活动的一部分,但我似乎无法扩展列表以填充可用的屏幕空间,然后滚动页面。 列表在那里并且可以滚动,但它只有 1 个列表项高。

谁能指点我正确的方向?

谢谢

*编辑

这是我试图将片段加载到的活动的布局

'<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false"
    android:orientation="vertical"
    android:padding="10dp"
    android:weightSum="1">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
        <ImageButton
            android:id="@+id/menuButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@null"
            android:contentDescription="@string/todo"
            app:srcCompat="@drawable/bars" />
        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="0dp"
            android:layout_weight="1"
            android:contentDescription="@string/todo"
            app:srcCompat="@drawable/fml_logo1" />
    </LinearLayout>

    <!--This should contain the fragment-->
    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

<!--MENU-->
<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/sideMenu"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#aa000000"
    android:visibility="gone">

    <LinearLayout
        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#ffffff"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintTop_toBottomOf="parent"
        >

        <ImageButton
            android:id="@+id/menuCloseButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@null"
            android:contentDescription="@string/todo"
            app:srcCompat="@drawable/bars" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_margin="10dp"
            android:text="Menu"
            android:textSize="30sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/menuHomeButton"
            android:layout_height="30dp"
            android:layout_width="match_parent"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:textSize="20sp"
            android:text="Home"
            />


        <TextView
            android:id="@+id/menuChangeProvButton"
            android:layout_height="30dp"
            android:layout_width="match_parent"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:textSize="20sp"
            android:text="Change GL/Province"
            />

    </LinearLayout>


  </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>'

我对此很陌生,因此非常感谢您的帮助。

【问题讨论】:

  • 您将其高度设置为 0。是否将其设置为 wrap_content 或 match_parent?
  • 这确实有效并使列表视图展开和滚动,但是它使片段全屏显示并且我失去了我所在的活动(因此标题和导航)
  • 这可能是我从 ListViewAdapter 膨胀列表视图的方式吗?这可能是我在 ListViewAdapter 中膨胀列表视图的方式吗? '查看 listViewItem = inflater.inflate(R.layout.prov_list_items, null, true);'
  • “失去活动”是什么意思?该片段的显示方式和位置?
  • 所以,我有一个活动,它有一个导航抽屉和一个标题。菜单将片段加载到布局中的 Frame 中。这似乎工作正常。当我将 ListView 的高度更改为 match_parent 时,它会使片段全屏显示。我显然对布局有问题,但我看不出它在哪里。对不起,我是新手。我将编辑问题以包含其加载到的主布局的代码。

标签: android android-layout


【解决方案1】:

如果您希望将片段绘制在所有活动内容之上,则需要将片段的容器放置在所有其他布局下方,以便在它们上方绘制,就像在您的 Activity 布局中一样。

...
</androidx.constraintlayout.widget.ConstraintLayout>
<FragmentContainerView  
    android:id="@+id/your_fragment_container"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    ..... />
</androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

    【解决方案2】:

    设法通过废弃布局并从头开始来解决问题。 仍然不太确定我最初做错了什么,但感谢您的帮助。

    【讨论】:

      猜你喜欢
      • 2011-07-15
      • 2019-01-25
      • 2012-01-26
      • 2017-04-04
      • 1970-01-01
      • 2022-06-15
      • 2013-09-25
      • 1970-01-01
      • 2023-03-06
      相关资源
      最近更新 更多