1

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

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

我尝试了很多东西,但都没有工作。我可以使每个项目具有相同的大小(bah,easy),将属性android:layout_weight="1"分配给每个listview项目并使用android:layout_height="0dp"。但是,listview使用的空间比屏幕更多。

因此,这里就是我的XML文件看起来像现在:

item_menu.xml(ListView项)

<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,因为我要处理滑动和点击事件)

+0

如果你有固定的4个项目,你可以尝试将它们添加到LinearLayout中,这将使事情变得更加简单 – Manish 2015-05-07 12:07:26

+0

好了,我不知道如果我得到它,但我使用的是线性布局为ListView,做我的自定义项目使用RelativeLayout的事实会影响它吗?我需要RelativeLayout,因为Item有一个以文本为中心的图像。 – 2015-06-02 17:59:26

回答

0

找出现在最好的方法是以编程方式设置布局,调用方法来获取显示大小,然后设置基于此的布局高度。

// 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 ;