ListView を使用する Android アプリケーションを作成していました。ListView は CheckedTextView リスト項目で構成されます。私のアクティビティには、上部に EditText があり、その下に TextView があり、下部に Button があります。TextView とボタンの間のスペースは、ListView が占める領域です。現在、私の ListView には項目がほとんど含まれていないため、ListView のスペース全体がそれらによって占有されていません。また、残されたスペースは明らかにデバイスによって異なります。私が知りたいのは、ListView に割り当てられたスペース全体がカバーされるように、ListView の行の高さを変更する方法があるかどうかです。また、いくつかのアイテムを追加する必要が生じた場合、行の高さはそれに応じて自動的に調整されます。私のレイアウトがどのようなものかを簡単に理解できるように、いくつかのコード スニペットを以下に示します。
activity_main.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="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#D0D0D0"
tools:context=".MainActivity" >
<Button
android:id="@+id/addcourse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Add Course" />
<EditText
android:id="@+id/coursenameet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:ems="10"
android:layout_marginBottom="2dp"
android:layout_marginTop="2dp"
android:hint="Course Name"
android:inputType="textCapWords" >
<requestFocus />
</EditText>
<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_below="@+id/coursenameet"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="@android:color/darker_gray" >
</View>
<TextView
android:id="@+id/weekdaytv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/divider"
android:layout_marginBottom="3dp"
android:layout_marginTop="3dp"
android:layout_centerHorizontal="true"
android:text="Select weekdays for this Course" >
</TextView>
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/addcourse"
android:layout_below="@+id/weekdaytv"
android:divider="#FFFFFF"
android:dividerHeight="2px">
</ListView>
</RelativeLayout>
項目は、BaseAdapter を使用して実装される CheckedTextViews です。BaseAdapter クラスの View メソッドは
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
int colorpos = position % colors.length;
if(v == null){
LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.listviewitem, null);
}
CheckedTextView newrow = (CheckedTextView) v.findViewById(R.id.checkedtextview);
v.setBackgroundColor(colors[colorpos]);
newrow.setTextColor(Color.BLACK);
newrow.setText(items[position]);
return v;
}
編集: メイン アクティビティのスニペット
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
adapter = new ItemsAdapter(MainActivity.this, weekdays, 6, lv.getBottom() - lv.getTop());
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
CheckedTextView ctv = (CheckedTextView) arg1;
adapter.toggle(ctv);
}
});