0

Android アプリ (API 8) にリストビューを実装しました。ただし、リストは 3 つの要素のみで構成されているため、画面の半分以上が空です。リストビューを拡張して、電話の画面全体をカバーしたいと考えています。同じことを行うにはどのような方法がありますか?

これが私のレイアウト XML ファイルです。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
android:background="@color/white"
android:shape="rectangle"
android:paddingTop="5dp"
android:paddingBottom="0dp"
>


<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
    android:weightSum="100"
    >

    <ImageView
        android:layout_weight="40"
        android:id="@+id/test_image"
        android:layout_width="158dp"
        android:layout_height="52dp"
        android:contentDescription="@string/footer"
        android:src="@drawable/logo1" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="127dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_weight="60" 
        android:prompt="@string/city_prompt"
        />
</LinearLayout>

  <LinearLayout android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"   
    >
    <ListView 
    android:id="@android:id/list"
    android:layout_weight="1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#000000"
    android:drawSelectorOnTop="true"
    android:layout_gravity="center"
    android:dividerHeight="2dp"
    />


</LinearLayout>


</LinearLayout>

リスト項目を画面全体に表示し、項目を 3 つだけ配置することに注意してください。

4

3 に答える 3

2

ListView はすべてのスペースを自動的に埋めるわけではありません。これを実現したい場合は、ここでは ListView がすべてのスペースを使用していますが、アイテムは使用していません。より大きな行を作成したい場合、項目が少ない場合は、getCount() メソッドを使用して getView メソッドでリスト項目の高さを指定します。

たとえば、リストの画面に一度に最大 6 つのアイテムを表示したいとします。次に、次のように ListView を実装します。

次のコードを onCreate メソッドに使用して、画面の幅と高さを取得します。

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

getView で、次の操作を行います。

int itemHeight = 0;
if (getCount() >= 6)
    itemHeight = height/6;
else
    itemHeight = height/getCount();

LinearLayout llView=new LinearLayout(YourActivityName.this);
//Or inflate through XML

llView.setLayoutParams(new LayoutParams(LayoutParams.Fill_Parent, itemHeight));
//Do other functionality
于 2012-06-15T04:37:53.760 に答える
2

XML をコピーして貼り付けて試してみたところ、問題なく動作しました。もう 1 つ伝えるべきことは、 を使用しないことlayout_height="wrap_content"です。代わりに、ListView が LinearLayout の残りの領域を埋めるようにするために、android:layout_height="0dip"既に使用したようにそれを与えることができます。android:layout_weight="1"

于 2012-06-15T04:41:19.377 に答える
1

最初の変更:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:background="@color/white"
    android:shape="rectangle"
    android:paddingTop="5dp"
    android:paddingBottom="0dp"
>

次に、次のように変更します。

<ListView 
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000"
    android:drawSelectorOnTop="true"
    android:layout_gravity="center"
    android:dividerHeight="2dp"
/>
于 2012-06-15T04:35:20.150 に答える