Android 如何在布局中将textview和edittext置于listview的顶部

Android 如何在布局中将textview和edittext置于listview的顶部,android,android-layout,android-widget,android-xml,Android,Android Layout,Android Widget,Android Xml,我试图在布局中将textview和edittext放在listview的顶部,但listview没有出现。当我删除这些textview和edittext时,就会出现listview 以下是我的布局xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orient

我试图在布局中将textview和edittext放在listview的顶部,但listview没有出现。当我删除这些textview和edittext时,就会出现listview

以下是我的布局xml:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:id="@+id/TestLinearLayout">
         <LinearLayout 
               android:orientation="horizontal"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"
               >
               <TextView  
                     android:layout_width="wrap_content" 
                     android:layout_height="wrap_content" 
                     android:text="@string/hello"
                     android:id="@+id/text"
               />   
               <EditText  
                     android:layout_width="fill_parent" 
                     android:layout_height="wrap_content"
                     android:id="@+id/editText"
               />   
        </LinearLayout>
        <ListView 
               android:id="@+id/ListView01" 
               android:layout_width="match_parent" 
               android:layout_height="fill_parent"
               android:layout_weight="1"
              />
</LinearLayout>

列表视图上方的线性布局占据了整个父视图,因为您已将高度指定为
填充父视图
so


将第二个线性布局的高度替换为
wrap\u content

列表视图上方的线性布局占用了整个父级,因为您将高度指定为
fill\u parent
so


将第二个线性布局的高度替换为
wrap\u content
更好地使用
RelativeLayout
,您可以在下面指定
android:layout\u=“@+id/header”
更好地使用
RelativeLayout
,并且可以在下面指定
android:layout=“@+id/header”

像这样改变

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

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

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textNoSuggestions" >

        <requestFocus />
    </EditText>
</LinearLayout>

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

 </LinearLayout>

像这样改变

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

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

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textNoSuggestions" >

        <requestFocus />
    </EditText>
</LinearLayout>

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

 </LinearLayout>

将第二个线性布局的高度替换为
包装内容

喜欢

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:id="@+id/TestLinearLayout">
         <LinearLayout 
               android:orientation="horizontal"
               android:layout_width="fill_parent"
               android:layout_height="wrap_content"
               >
               <TextView  
                     android:layout_width="wrap_content" 
                     android:layout_height="wrap_content" 
                     android:text="@string/hello"
                     android:id="@+id/text"
               />   
               <EditText  
                     android:layout_width="fill_parent" 
                     android:layout_height="wrap_content"
                     android:id="@+id/editText"
               />   
        </LinearLayout>
        <ListView 
               android:id="@+id/ListView01" 
               android:layout_width="match_parent" 
               android:layout_height="fill_parent"
               android:layout_weight="1"
              />
</LinearLayout>

将第二个线性布局的高度替换为
包装内容

喜欢

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:id="@+id/TestLinearLayout">
         <LinearLayout 
               android:orientation="horizontal"
               android:layout_width="fill_parent"
               android:layout_height="wrap_content"
               >
               <TextView  
                     android:layout_width="wrap_content" 
                     android:layout_height="wrap_content" 
                     android:text="@string/hello"
                     android:id="@+id/text"
               />   
               <EditText  
                     android:layout_width="fill_parent" 
                     android:layout_height="wrap_content"
                     android:id="@+id/editText"
               />   
        </LinearLayout>
        <ListView 
               android:id="@+id/ListView01" 
               android:layout_width="match_parent" 
               android:layout_height="fill_parent"
               android:layout_weight="1"
              />
</LinearLayout>

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/TestLinearLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/firstlayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello" />

        <EditText
            android:id="@+id/editText"
            android:layout_below="@+id/text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </RelativeLayout>

    <ListView
        android:id="@+id/ListView01"
        android:layout_below="@+id/firstlayout"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1" />

</RelativeLayout>

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/TestLinearLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/firstlayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello" />

        <EditText
            android:id="@+id/editText"
            android:layout_below="@+id/text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </RelativeLayout>

    <ListView
        android:id="@+id/ListView01"
        android:layout_below="@+id/firstlayout"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1" />

</RelativeLayout>

您还可以向
列表视图添加标题视图
,这正是您想要的(我想)

您还可以在
列表视图中添加标题视图,这正是您想要的(我想)

非常感谢。。你能告诉我这有什么用吗?这是用来防止应用程序启动时出现软键盘。非常感谢。。你能告诉我这里有什么用吗?这是用来防止应用程序启动时出现软键盘。使用
addHeaderLayout
向ListView添加标题。查看我的回答。使用
addHeaderLayout
向列表视图添加标题。请看我的回答。