2017-07-01 52 views
0

我有一個ListView,我使用適配器陣列填充它。我想在那裏添加一個搜索欄,但是當我添加一個搜索欄時,它會重複它,所以我最終創建了兩個佈局文件:一個用於搜索欄,另一個用於項目列表。在項目列表佈局上放置搜索欄

的ListView在我的Android:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:paddingLeft="8dp" 
    android:paddingRight="16dp"> 

    <ImageView 
     android:id="@+id/icon" 
     android:layout_width="40dp" 
     android:layout_height="70dp" 
     android:background="@color/bckground" 
     android:src="@drawable/placeholder" /> <!-- ree image--> 


    <LinearLayout 
     android:id="@+id/textContainer" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/icon" 
     android:orientation="vertical" 
     android:paddingTop="16dp"> 

     <TextView 
      android:id="@+id/name" 
      android:layout_width="match_parent" 
      tools:text="name" /> 

     <TextView 
      android:id="@+id/surname" 
      android:layout_width="match_parent"  
      tools:text="surname" /> 

    </LinearLayout> 

ListView控件佈局打印屏幕:

enter image description here

搜索欄文件格式:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

     <EditText 
      android:id="@+id/searchtext" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:ems="10" 
      android:inputType="text" 
      android:text="Search.." /> 

     <Button 
      android:id="@+id/sumbit" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Search" /> 
    </LinearLayout> 

搜索欄佈局打印屏幕:

enter image description here

我想有一個佈局的搜索欄和項目清單。 如何將這兩個佈局文件組合在一起?

回答

0

在包含搜索欄的佈局文件中添加您的列表視圖。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

    <EditText 
     android:id="@+id/searchtext" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:inputType="text" 
     android:text="Search.." /> 

    <Button 
     android:id="@+id/sumbit" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Search" /> 

    <!--Add your list view here--> 
    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

</LinearLayout> 
0

我想有一個佈局的搜索欄和項目清單。我怎樣才能將這兩個佈局文件結合在一起?

你不行。這些概念上是兩種不同的佈局。一個描述列表中的每個項目,它由ListView適配器提供,另一個描述頂級視圖,其中包含ListView以及您希望作爲該佈局一部分的任何其他視圖。