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 ;