【问题标题】:Android - Make listview fill screen and list items to fill available spaceAndroid - 使列表视图填充屏幕并列出项目以填充可用空间
【发布时间】:2015-02-11 03:58:59
【问题描述】:

所以基本上我想要完成的是让我的屏幕充满一个列表视图,其中正好有 4 个不可变项(菜单项)。为此,我创建了一个自定义适配器和一个包含使用此适配器的列表视图的活动。

基本上我想让 4 个项目填满屏幕,所以每个项目应该占据 25% 的空间(不包括操作栏的区域)。

我尝试了多种方法,但都没有奏效。我可以使每个项目都具有相同的大小(呸,很简单),分配属性android:layout_weight="1" 并将android:layout_height="0dp" 分配给每个列表视图项目。但是,列表视图使用的空间比屏幕多。

这就是我的 xml 文件现在的样子:

item_menu.xml(列表视图项)

<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="0dp"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:layout_weight="1"
    tools:context="com.android.franzoni.atlas.views.CatalogActivity" >

<ImageView
    android:id="@+id/imgMenuIcon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="15dp"
    android:layout_marginBottom="15dp"
    android:adjustViewBounds="true"
    android:src="@drawable/ic_menu_profile" />

<TextView
    android:id="@+id/txtMenuTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_marginLeft="30dp"
    android:layout_toRightOf="@+id/imgMenuIcon"
    android:text="Menu Title" />

</RelativeLayout>

activity_menu.xml(活动的布局)

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="0dp"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="0dp"
    tools:context="com.android.franzoni.atlas.views.MenuActivity" >
<ListView
    android:id="@+id/lvMenu"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</ListView>

</LinearLayout>

你们知道怎么做吗?有没有更简单的方法来进行菜单活动? (我想到了 listview,因为我要处理滑动和点击事件)

【问题讨论】:

  • 如果你已经修复了 4 个项目,你可以尝试将它们添加到 LinearLayout 中,这会让事情变得更容易
  • 好吧,我不知道我是否明白,但我正在为 ListView 使用线性布局,我的自定义项目使用 RelativeLayout 的事实是否会影响它?我需要 RelativeLayout,因为该项目将有一个以文本为中心的图像。

标签: android android-layout android-listview baseadapter android-layout-weight


【解决方案1】:

目前最好的方法是通过编程设置布局,调用一个方法来获取显示大小,然后根据它设置布局高度。

// Get Screen Size
DisplayMetrics metrics = new DisplayMetrics ();
context.WindowManager.DefaultDisplay.GetMetrics (metrics);

// Set layout based on screen size and qty of items
ll.LayoutParameters.Height = (metrics.HeightPixels - topbarSize) / items.Count ;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-10
    • 2014-01-05
    相关资源
    最近更新 更多