2

我想创建一个活动,左侧有一个 ListView,右侧有一个 TextView,并排。我写了下面的xml,但是ListView占据了整个页面,不用担心wrap_content。为什么?我该如何解决?

<LinearLayout 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:orientation="horizontal" >

    <ListView 
        android:id="@+id/lv"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />

    <TextView 
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />

</LinearLayout>

编辑:我的 onCreate

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView lv = (ListView) findViewById(R.id.lv);
    String[] values = new String[] { "Test1", "Test2", "Test3", "Test4" };
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.left, R.id.tv1, values);
    lv.setAdapter(adapter); 
}
4

2 回答 2

0

From what you described wrap_content seems to be stretching right across the screen, so let's set a limit where the ListView and TextView share the screen 50/50:

<LinearLayout 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:orientation="horizontal" >

    <ListView 
        android:id="@+id/lv"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <TextView 
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:minWidth="200dp"
        android:layout_weight="1" />

</LinearLayout>
于 2012-11-16T22:12:05.253 回答
0

编辑:试试这个

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:text="TextView" />

    <ListView
        android:id="@+id/lv"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@id/tv" >

    </ListView>

</RelativeLayout>
于 2012-11-16T22:13:15.877 回答