2013-02-19 70 views
1

那麼你好!Android佈局 - ListView佔用太多空間

我遇到了Android中的問題(我也是Android的新手)。

首先,我有2片段。 1 Fragment包含一個ListView,用於在手機上顯示音樂。第二片段有一些控制。

我希望帶有控件的片段停留在屏幕的底部。 ListFragment應該堅持活動的頂部。我怎樣才能做到這一點?

現在,我已經決定了XML中的以下內容: activity_main.xml中:

<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="match_parent" 
android:background="@drawable/rain" 
android:gravity="center|top" 
android:orientation="vertical" 
tools:context=".MainActivity" > 

<!-- Playlist --> 
<fragment 
    android:id="@+id/playlist_fragment" 
    android:layout_width="fill_parent" 
    android:layout_height="0px" 
    android:layout_gravity="top" 
    android:layout_weight="6" 
    class="at.wireless.musicstream.fragment.PlaylistFragment" /> 

<!-- Separator --> 
<View 
    android:layout_width="match_parent" 
    android:layout_height="2dp" 
    android:background="@android:color/holo_blue_dark" /> 

<!-- Controls --> 
<fragment 
    android:id="@+id/control_fragment" 
    android:layout_width="fill_parent" 
    android:layout_height="0px" 
    android:layout_weight="1" 
    class="at.wireless.musicstream.fragment.ControlFragment" /> 
    </LinearLayout> 

我用layout_weight這裏,但是這不是我想要的,因爲在不同的屏幕尺寸,這也將佔用不同高度。但我想控制有一個 「固定」 的大小(我用DP的片段)

playlist_fragment.xml:

<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="wrap_content" 
android:gravity="center|top" 
tools:context=".MainActivity" > 

<ListView 
    android:id="@android:id/list" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" > 
</ListView> 
</LinearLayout> 

而且我control_fragment.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:background="@android:color/transparent" 
android:orientation="horizontal" 
tools:context=".MainActivity" > 



<ImageView 
    android:id="@+id/albumView" 
    android:layout_width="70dp" 
    android:layout_height="70dp" 
    android:layout_gravity="left" 
    android:background="@drawable/no_album_art" 
    android:contentDescription="Album Art" /> 

<LinearLayout android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <TextView android:id="@+id/actPlayingTitle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="14sp" 
     android:textIsSelectable="false" 
     android:text="hier titel" 
     android:textColor="@android:color/white"/> 
    <TextView android:id="@+id/actPlayingArtist" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textIsSelectable="false" 
     android:textSize="12sp" 
     android:text="hier artist" 
     android:textColor="@android:color/white" />" 

</LinearLayout> 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="right" 
    android:layout_marginLeft="20dp" > 

    <Button 
     android:id="@+id/rewindBtt" 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:layout_gravity="right" 
     android:background="@drawable/play" /> 

    <Button 
     android:id="@+id/playbackBtt" 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:layout_gravity="right" 
     android:background="@android:color/black" /> 

    <Button 
     android:id="@+id/skipBtt" 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:layout_gravity="right" 
     android:background="@android:color/white" /> 
</LinearLayout> 
</LinearLayout> 

Image

ListView應該佔用control_fragment離開的空間。所以可以說我有一個總屏幕高度爲1000.控件佔用200,然後ListView剩下800。

+0

你可以發佈可繪製的佈局如何你想要的?只顯示有問題的圖片 – Pratik 2013-02-19 11:08:06

+0

已添加圖片和示例 – Kafioin 2013-02-19 11:31:37

回答

1

切換到RelativeLayout,這將允許您的控件保持在一個固定的位置,您還需要爲控件定義一個高度(我已將它設置爲200dp)。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@drawable/rain" 
android:gravity="center|top" 
tools:context=".MainActivity" > 

<!-- Playlist --> 
<fragment 
    android:id="@+id/playlist_fragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="top" 
    class="at.wireless.musicstream.fragment.PlaylistFragment" 
    android:layout_above="@+id/separator"/> 

<!-- Separator --> 
<View 
    android:id="@+id/separator" 
    android:layout_width="match_parent" 
    android:layout_height="2dp" 
    android:layout_above="@+id/control_fragment" 
    android:background="@android:color/holo_blue_dark" /> 

<!-- Controls --> 
<fragment 
    android:id="@+id/control_fragment" 
    android:layout_width="fill_parent" 
    android:layout_height="200dp" 
    android:layout_alignParentBottom="true" 
    class="at.wireless.musicstream.fragment.ControlFragment" /> 
    </RelativeLayout> 
+0

哇!我已經使用了RelativeLayout,但我忘了添加layout_alingParentBottom。咩!它總是小事。感謝您的回答! – Kafioin 2013-02-19 11:47:42

+0

您可以編輯您的解決方案,並用RelativeLayout替換LinearLayout – Kafioin 2013-02-19 11:48:09

+0

哈忘記實施自己的更改...感謝您的支持! – Ljdawson 2013-02-19 11:59:13