簡體   English   中英

Android-創建列表視圖填充屏幕並列出項目以填充可用空間

[英]Android - Make listview fill screen and list items to fill available space

因此,基本上我想完成的事情是使我的屏幕充滿一個恰好具有4個不可變項(菜單項)的列表視圖。 為此,我創建了一個自定義適配器和一個活動,該活動包含使用此適配器的列表視圖。

基本上,我希望屏幕上有4個項目,因此每個項目都應占據25%的空間(不包括操作欄的區域)。

我嘗試了多種方法,但均無效果。 我可以使每個項目都具有相同的大小(簡單,容易),將屬性android:layout_weight="1"分配給每個listview項目,並使用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,因為我要處理滑動和單擊事件)

弄清楚目前最好的方法是以編程方式設置布局,調用一種方法來獲取“顯示尺寸”,然后根據此設置布局高度。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM