【问题标题】:Android Layout - ListView taking too much spaceAndroid 布局 - ListView 占用太多空间
【发布时间】:2013-02-04 00:39:31
【问题描述】:

你好!

我在 Android 中遇到了问题(我也是 Android 新手)。

首先,我有 2 个片段。 1 Fragment 包含一个 ListView,我在其中显示手机上的音乐。第二个片段有一些控件。

我希望带有控件的片段保持在屏幕底部。 ListFragment 应该贴在 Activity 的顶部。我该怎么做?

现在我在 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,但这不是我想要的,因为在不同的屏幕尺寸下,这也会占用不同的高度。但我希望控件具有“固定”大小(我在 Fragment 中使用 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。

【问题讨论】:

  • 你能发布你想要的可绘制布局吗?只显示有问题的图片
  • 已添加图片和示例

标签: android listview layout android-activity fragment


【解决方案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>

【讨论】:

  • 哇,谢谢!我已经使用了RelativeLayout,但我忘了添加layout_alingParentBottom。呸!总是小事。感谢您的回答!
  • 您可以编辑您的解决方案并将 LinearLayout 替换为 RelativeLayout
  • 忘了实施我自己的更改...感谢您的提醒!