Android ListView和wrap_内容

Android ListView和wrap_内容,android,xml,listview,Android,Xml,Listview,我想创建一个活动,左侧是ListView,右侧是TextView,并排显示。我编写了下面的xml,但是ListView占据了整个页面,它并不担心wrap_内容。为什么?我如何解决它 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_

我想创建一个活动,左侧是ListView,右侧是TextView,并排显示。我编写了下面的xml,但是ListView占据了整个页面,它并不担心wrap_内容。为什么?我如何解决它

<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); 
}
@覆盖
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv=(ListView)findViewById(R.id.lv);
字符串[]值=新字符串[]{“Test1”、“Test2”、“Test3”、“Test4”};
ArrayAdapter=新的ArrayAdapter(这个,R.layout.left,R.id.tv1,值);
低压设置适配器(适配器);
}

从您所描述的内容来看,内容似乎在整个屏幕上延伸,因此让我们设置ListView和TextView共享屏幕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>

编辑:试试这个

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


它可以工作,但我不知道哪个是正确的值,我需要包装内容“正确的值”是什么意思<代码>dp将根据屏幕大小进行缩放。如果需要它与单独的行一致,则需要适当地调整row.xml或行代码的大小,并且可能依赖于“wrap_content”。只是预览不会显示它,因为它不知道其中有什么。这意味着listview行中的文本长度未知,并且存在将值设置得太低而断行或太高并带有大量无用空间的风险。您是否将此布局传递给
setContentView()
?点击问题左下角的“编辑”,发布你的
onCreate()
方法。哇,ListView真的不喜欢共享。如果您将我的代码更改为两个ListView,它们会像应该的那样分割屏幕。。。无论如何,我添加了
minWidth
属性,也许这是您想要的?不要使用wrap\u内容查看listview。。看见