【问题标题】:Placing Fragment below Listview在 Listview 下方放置 Fragment
【发布时间】:2017-07-02 15:01:10
【问题描述】:

我正在为我的 Android 布局而苦苦挣扎。

我想创建一个线性布局,它在顶部包含一个按钮,接下来是一个列表视图,当显示列表视图的所有元素时,我想显示一个显示谷歌地图的片段。

这是我的布局 xml 文件。在这种情况下,您只能看到 Fragment 和 Button 而看不到列表视图

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:paddingLeft="10dp"
              android:paddingRight="10dp"
              android:orientation="vertical"
        tools:context="de.mypackage.MyActivity">

    <Button
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:layout_weight="0"
            android:id="@+id/button_back"
            android:text="@string/back"
            />

        <ListView
                android:id="@+id/list"
                android:layout_height="fill_parent"
                android:layout_width="fill_parent"
                android:layout_weight="1"/>
                />

    <fragment
            android:id="@+id/fragement"
            android:name="de.mpackage.MapsFragment"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:layout_weight="0"
            tools:layout="@layout/fragment_maps"/>

</LinearLayout>

知道如何实现我的布局吗?

【问题讨论】:

  • 你可以将片段作为页脚添加到列表视图吗?
  • 将标题添加到将保留按钮的列表视图。将页脚添加到将包含 Google 地图的列表视图。
  • @MichaelMeyer 我尝试将wrap_content 用于ListView,然后将layout_weight=1 放入fragment。我发布了答案。希望能帮助到你。 :)

标签: android android-fragments layout


【解决方案1】:

试试这个代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingLeft="10dp"
    android:paddingRight="10dp">

    <Button
        android:id="@+id/button_back"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/back" />

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <fragment
        android:id="@+id/fragement"
        android:name="com.yougotag.supplierfieldagent.fragments.AboutFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:layout="@layout/fragment_about_app" />

</LinearLayout>

【讨论】:

  • 抱歉,我还是看不到列表视图。只有谷歌地图的片段
【解决方案2】:

建议: 将您的根布局声明为 RelativeLayout

RelativeLayout 是一个视图组,以相对的方式显示子视图 职位。每个视图的位置可以指定为相对于 兄弟元素。

然后使用 android:layout_below

将此视图的上边缘定位在给定锚视图 ID 下方。 容纳此视图的上边距和锚点视图的下边距。

<fragment
        android:id="@+id/fragement"
        android:name="de.mpackage.MapsFragment"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_below="@+id/birth"
        tools:layout="@layout/list"/>

【讨论】:

    【解决方案3】:
    Try this code
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingLeft="10dp"
        android:weightSum="1"
        android:paddingRight="10dp">
    
        <Button
            android:id="@+id/button_back"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/back" />
    
        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.5" />
    
        <fragment
            android:id="@+id/fragement"
            android:name="com.yougotag.supplierfieldagent.fragments.AboutFragment"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.5"
            tools:layout="@layout/fragment_about_app" />
    
    </LinearLayout>
    
    this will set both in half screen you can change ration of weights in layout.
    

    【讨论】:

    • 对不起,这不是我要找的
    【解决方案4】:

    试试这个:

    <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                      xmlns:tools="http://schemas.android.com/tools"
                      android:layout_width="wrap_content"
                      android:layout_height="fill_parent"
                      android:paddingLeft="10dp"
                      android:paddingRight="10dp"
                      tools:context="de.mypackage.MyActivity">
    
                 <Button
                    android:layout_height="wrap_content"
                    android:layout_width="fill_parent"
                    android:layout_weight="0"
                    android:id="@+id/button_back"
                    android:text="@string/back"
                    />
    
                <ListView
                        android:id="@+id/list"
                        android:layout_height="fill_parent"
                        android:layout_width="fill_parent"
                        android:layout_weight="1"
                        android:layout_below="@+id/button_back"/>
                        />
    
            <fragment
                    android:id="@+id/fragement"
                    android:name="de.mpackage.MapsFragment"
                    android:layout_height="wrap_content"
                    android:layout_width="fill_parent"
                    android:layout_weight="0"
                    android:layout_below="@+id/list"
                    tools:layout="@layout/fragment_maps"/>
    
        </RelativeLayout>
    

    【讨论】:

    • 现在我只能看到列表视图
    【解决方案5】:

    我找到了解决方案,希望对您有所帮助。 在我的ListView 中为height 而不是使用0dp 我使用wrap_content 并为我的layout_weight=1 添加Fragment

    <LinearLayout
        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="#EFEFEF"
        android:orientation="vertical">
    
         <Button
            android:id="@+id/button_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            />
    
         <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:divider="@null"/>
    
        <fragment  
            android:name="de.mpackage.MapsFragment"
            android:id="@+id/fragement"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_margin="5dp"
            android:layout_weight="1"
            />
    
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-16
      • 1970-01-01
      • 1970-01-01
      • 2015-12-20
      • 1970-01-01
      • 2011-10-16
      • 1970-01-01
      相关资源
      最近更新 更多