Android:带图像和文字的搜索结果列表视图

问题描述:

在我正在开发的android 4.1应用程序中,我希望我的文本在搜索结果列表视图中显示在缩略图的右侧。我通过解析一个xml文件来得到结果。现在我的文字出现在图像的下方,但我希望它出现在图像的右侧。Android:带图像和文字的搜索结果列表视图

people_search_list.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"> 

    <ListView android:id="@android:id/list" 
     android:layout_width="fill_parent" android:layout_height="0dip" 
     android:layout_weight="1" android:drawSelectorOnTop="false" 
     /> 

    <ImageView 
     android:id="@+id/image" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom" 
     android:contentDescription="@string/description" 
     android:gravity="left" 
     android:scaleType="fitXY" /> 

     <TextView 
     android:id="@+id/empty" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_gravity="left" 
     android:text="" /> 

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

    <LinearLayout 
     android:id="@+id/footer" 
     style="@android:style/ButtonBar" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/prev_10_results" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.14" 
      android:text="@string/prev_10_results" /> 

     <Button 
      android:id="@+id/next_10_results" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.5" 
      android:text="@string/next_10_results" /> 
    </LinearLayout> 

    </ScrollView> 

    </LinearLayout> 

PeopleSearchAdapter.java(这是我从一个XML文件中有牵强之后显示的内容)

public PeopleListView (Context context, String itemName, String itemLocation, String image_url, String kudoRank) 
     { 
      super (context); 
      setOrientation(LinearLayout.VERTICAL); 

      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 
        ViewGroup.LayoutParams.WRAP_CONTENT); 
      params.setMargins(5, 3, 5, 0); 

      image = new ImageView (context); 
      Log.v("Image URL: ", image_url); 
      if(image_url != null && !(image_url.equals(""))) 
      { 
       try 
       { 
        URL url = new URL (image_url); 
        URLConnection conn = url.openConnection(); 
        conn.connect(); 
        BufferedInputStream bis = new BufferedInputStream(conn.getInputStream()); 
        Bitmap bm = BitmapFactory.decodeStream (bis); 
        bis.close(); 
        image.setImageBitmap(bm); 
       } 
       catch (IOException e) 
       { 
        e.printStackTrace(); 
       } 
      } 

      else 
      { 
       image.setImageResource(R.drawable.no_image); 
      } 
      addView (image, params); 

      name = new TextView (context); 
      name.setText(itemName); 
      name.setTextSize(16f); 
      name.setTextColor(Color.BLACK); 
      addView(name, params); 

      kudo_rank = new TextView (context); 
      kudo_rank.setText(kudoRank); 
      kudo_rank.setTextSize(16f); 
      kudo_rank.setTextColor(Color.BLACK); 
      addView(kudo_rank, params); 

      location = new TextView (context); 
      location.setText(itemLocation); 
      location.setTextSize(16f); 
      location.setTextColor(Color.BLACK); 
      addView(location, params); 

     } 

I've attached a screenshot of how the results are being displayed now and just want the three text fields that are displayed below the image right now to be displayed to its right instead. 

search results view

谢谢..

最简单的方法是在ListView xml中定义您的布局。这是我用过的。我做了一个应用程序类似的东西,希望它有助于:

这是显示在列表视图:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/view" > 

    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="50dp" 
     android:background="@drawable/logo" 
     android:orientation="vertical" > 
    </LinearLayout> 

    <com.google.ads.AdView 
      android:id="@+id/adView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      ads:adSize="BANNER" 
      ads:adUnitId="a15009789e230a7" /> 
    <ListView 
     android:id="@android:id/list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/linearLayout1" 
     android:layout_above="@+id/adView" 
     android:layout_centerHorizontal="true" 
     android:background="@drawable/background" 
     android:cacheColorHint="#00000000" > 
    </ListView> 






</RelativeLayout> 

这是ListView定义,其中,类似于你想要的布局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" 
    android:padding="10dp" > 

    <ImageView 
     android:id="@+id/imgThumb" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginBottom="5dp" 
     android:layout_marginRight="15dp" 
     android:layout_marginTop="5dp" 
     android:background="@drawable/diningbutton" 
     android:gravity="center_vertical" /> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@+id/imgThumb" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/diningTitle" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentTop="true" 
      android:gravity="top" 
      android:paddingTop="5dp" 
      android:textColor="#000000" 
      android:textSize="16dp" 
      android:textStyle="normal" /> 

     <TextView 
      android:id="@+id/listSubHeader" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:gravity="bottom" 
      android:paddingBottom="5dp" 
      android:paddingTop="5dp" 
      android:textColor="#000000" 
      android:textSize="14dp" 
      android:textStyle="italic" /> 
    </LinearLayout> 

</RelativeLayout> 

这是适配器:

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 

    super.onCreate(savedInstanceState); 
    this.setContentView(R.layout.dining); 



    // Create ListData object to hold list data 
    ListData newListItem = new ListData(); 

    try 
    { 
     // parse data and populate the list view 
     this.itemObject = newListItem.getData(DATAURL); 
     Log.e(TAG, "item object: " + this.itemObject.toString()); 
     ListAdapter adapter = new ListAdapter(this, R.layout.dininglistview, this.itemObject); 
     // Create listview 
     this.listView1 = (ListView) this.findViewById(android.R.id.list); 
     this.listView1.setAdapter(adapter); 
     GetImagesTask imgTask = new GetImagesTask(); 
     imgTask.execute(this.itemObject); 

     AdView adView = (AdView)this.findViewById(R.id.adView); 
     AdRequest re = new AdRequest(); 
     adView.loadAd(re); 

    } 
    catch (InterruptedException e) 
    { 

     Log.e(TAG, "Unexpected error", e); 
    } 
    catch (ServiceException e) 
    { 
     AlertDialog alertDialog = new AlertDialog.Builder(DiningActivity.this).create(); 
     alertDialog.setTitle("Temporarily unavailable"); 
     alertDialog.setMessage("Please contact [email protected]"); 
     alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
      // if there is an error, send the user to the home page 
      @Override 
      public void onClick(DialogInterface dialog, int which) 
      { 
       Intent homeIntent = new Intent(DiningActivity.this, HomeActivity.class); 
       DiningActivity.this.startActivity(homeIntent); 

      } 
     }); 
     alertDialog.show(); 
    } 
    // implement dining adapter 

} 

我希望这有助于!祝你好运!