5

私はリストビュー用にこのxmlレイアウトを持っています:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/menu"
android:orientation="vertical" android:background="#2f4f4f" android:layout_width="wrap_content" android:layout_height="fill_parent">

  <ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:background="#2f4f4f" android:cacheColorHint="#2f4f4f" android:scrollbars="none">
  </ListView>
</LinearLayout>

線形レイアウトは、デフォルトで全画面幅を占有します。

ここに画像の説明を入力

プログラムで変更したい.次のようなものを作りたい:

ここに画像の説明を入力

しかし、linearlayout の幅を変更しても何も起こらず、画面全体の幅を占めています。

 LayoutInflater inflater = LayoutInflater.from(this);
 View menu = inflater.inflate(R.layout.xml_layout, null);
 menu.setLayoutParams(new LayoutParams(20,LayoutParams.WRAP_CONTENT));

リストビューでレイアウト幅を次のようにハードコーディングした後:

 <ListView
  android:id="@+id/list"
  android:layout_width="200dp"
  android:layout_height="wrap_content"
  android:background="#2f4f4f"
  android:cacheColorHint="#2f4f4f"
  android:scrollbars="none" >

</ListView>

私はこれを得る:

ここに画像の説明を入力

これを行う方法を提案してください。

4

2 に答える 2

4

以下に示すようにリストビューを変更し、XML でレイアウト幅をハードコードします。

<?xml version="1.0" encoding="utf-8"?>
<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="@android:color/white">


<ListView
    android:id="@+id/list"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:background="#2f4f4f"   >

</ListView>

</LinearLayout>

線形レイアウトリストビュー fill_parentで Wrap_content を実行していたため、リストビュー、つまりフルスクリーンをラップしている linearlayout です。

于 2012-12-29T04:37:25.150 に答える
2

複数の画面サイズに合わせたい場合は、空のレイアウトを追加してウェイトを使用することをお勧めします。

このような:

<?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="horizontal" android:background="@android:color/white">


<ListView
    android:id="@+id/list"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="3"
    android:background="#2f4f4f"   >

</ListView>

<LinearLayout android:layout_width="0dp"
android:layout_height="fill_parent" 
android:layout_weight="1"
android:orientation="horizontal" android:background="@android:color/white">

</LinearLayout>

私はただ重みを目で追っただけです。必要な幅に調整します。このようにすると、画面サイズごとにサイズをハードコーディングする必要がなくなります。

于 2012-12-29T06:49:50.497 に答える