【问题标题】:How to put textview and edittext top of listview in layout如何将listview的textview和edittext放在布局中
【发布时间】:2012-09-07 11:06:21
【问题描述】:

我尝试将 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>

【问题讨论】:

  • 使用addHeaderLayout 将标题添加到ListView。查看我的回复。

标签: android android-layout android-widget android-xml


【解决方案1】:

这样改

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

【讨论】:

  • 非常感谢..你能告诉我这里的有什么用吗?
  • 这个用来防止应用程序启动时出现软键盘。
【解决方案2】:

最好使用RelativeLayout,可以指定android:layout_below="@+id/header"

【讨论】:

    【解决方案3】:

    您还可以向ListView 添加标题视图,这正是您想要的(我认为)
    See Documentation here.

    【讨论】:

      【解决方案4】:

      列表视图上方的线性布局占据了整个父级,因为您将高度指定为fill_parent 所以,

      将第二个线性布局的高度替换为wrap_content

      【讨论】:

        【解决方案5】:
         <?xml version="1.0" encoding="utf-8"?>
              <RelativeLayout 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"
             android:layout_alignParentTop="true"
             android:id="@+id/layout"
             >
             <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_below="@id/layout/>
             </RelativeLayout>
        

        【讨论】:

          【解决方案6】:

          将第二个线性布局的高度替换为wrap_content

          喜欢

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

          【讨论】:

            【解决方案7】:

            试试这个:

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-05-28
              • 1970-01-01
              • 1970-01-01
              • 2015-10-11
              相关资源
              最近更新 更多