2017-02-14 156 views
1

我正在努力使用我的Android佈局。在列表視圖下放置片段

我想創建一個線性佈局,其中包含頂部的按鈕,下一個列表視圖,並且當顯示列表視圖的所有元素時,我想顯示顯示Google地圖的片段。

這是我的Layout xml文件。在這種情況下,你只能看到片段和按鈕,但不是列表視圖

<?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> 

任何想法我如何能實現我的佈局?

+1

增加layout_weight=1你可以添加片段作爲註腳到ListView? – raktale

+0

將標題添加到將保存按鈕的列表視圖中。將頁腳添加到您的列表視圖中,該視圖將保存Google地圖。 – nnn

+0

@MichaelMeyer我嘗試使用'wrapper_content'作爲'ListView',並將'layout_weight = 1'放入'fragment'中。我發佈了答案。希望能幫助到你。 :) – natsumiyu

回答

0

試試這個代碼

<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> 
+0

對不起,但我仍然無法看到列表視圖。只有與谷歌地圖片段 –

0

建議: 聲明根佈局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"/> 
1
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. 
+0

對不起,這不是我正在尋找 –

0

試試這個:

<?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> 
+0

現在我只能看到列表視圖 –

0

我找到了解決辦法希望它幫助。 在我ListViewheight,而不是使用0dp我以前wrap_content和我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>