2014-01-26 35 views
0

我正嘗試在列表視圖的頂部使用我的產品標題PNG創建一個簡單的佈局。像enter image description here我很喜歡這個layout.xml。我們如何才能在Android的列表視圖的頂部放置圖像橫幅?

<?xml version="1.0" encoding="utf-8"?> 

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


    <TextView 
    android:id="@+id/label" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@+id/label" 
    android:textSize="50px" /> 

    </RelativeLayout> 

我試圖把這個橫幅放置在相對佈局上面的線性佈局,但它是不允許的,我看到這個UI。 enter image description here 我們如何將圖像橫幅放在列表視圖的頂部?

這是我的主要活動。我正在嘗試創建登錄頁面。

 package com.test.abc; 

    import java.io.IOException; 
    import java.io.InputStream; 
    import java.util.ArrayList; 
    import java.util.Iterator; 
    import java.util.LinkedHashMap; 
    import java.util.Set; 

    import org.xmlpull.v1.XmlPullParser; 
    import org.xmlpull.v1.XmlPullParserException; 
    import org.xmlpull.v1.XmlPullParserFactory; 

    import android.app.ListActivity; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.widget.ListView; 

    import android.widget.Toast; 

    import android.widget.ArrayAdapter; 

    public class MainActivity extends ListActivity { 



     public void onCreate(Bundle icicle) { 
      super.onCreate(icicle); 
      String[] values = new String[8]; 
      XmlPullParserFactory pullParserFactory;  
      try { 
       pullParserFactory = XmlPullParserFactory.newInstance(); 
       XmlPullParser parser = pullParserFactory.newPullParser(); 
        InputStream in_s = getApplicationContext().getAssets().open("Credentials.xml");       
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false); 
        parser.setInput(in_s, null); 
        values = parseXML(parser); 
      } catch (XmlPullParserException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
       R.layout.activity_main, R.id.label, values); 
      setListAdapter(adapter); 
      } 

      @Override 
      protected void onListItemClick(ListView l, View v, int position, long id) { 
      String item = (String) getListAdapter().getItem(position); 
      Toast.makeText(this, item + " selected ", Toast.LENGTH_LONG).show(); 

      } 

      private String [] parseXML(XmlPullParser parser) throws XmlPullParserException,IOException 
      { 
       ArrayList<UserName> userNameAL = null; 
       int eventType = parser.getEventType(); 
       UserName currentUserName = null; 
       while (eventType != XmlPullParser.END_DOCUMENT){ 
        String name = null; 
        switch (eventType){ 
         case XmlPullParser.START_DOCUMENT: 
          userNameAL = new ArrayList<UserName>(); 
          break; 
         case XmlPullParser.START_TAG: 
          name = parser.getName(); 
          if (name.equals("user")){ 
           currentUserName = new UserName(); 

          } else if (currentUserName != null){ 
           if (name.equals("userName")){ 
            currentUserName.userName = parser.nextText(); 
           } else if (name.equals("userPass")){ 
            currentUserName.userPass = parser.nextText(); 
           } 
          } 
          break; 
         case XmlPullParser.END_TAG: 
          name = parser.getName(); 
          if (name.equalsIgnoreCase("user") && currentUserName != null){ 
           userNameAL.add(currentUserName); 
          } 
        } 
        eventType = parser.next(); 
       } 

       Set<String> keys = printUserNames(userNameAL).keySet(); 
       String [] values = keys.toArray(new String[8]); 
    /*   for(String k:keys){ 
        System.out.println(k+" -- "+printUserNames(userNameAL).get(k));   
       }*/ 

       return values; 

      } 

      private LinkedHashMap<String, String> printUserNames(ArrayList<UserName> userNameAL) 
      { 
       LinkedHashMap<String, String> lhm = new LinkedHashMap<String, String>();  
       Iterator<UserName> it = userNameAL.iterator(); 
       while(it.hasNext()) 
       { 
        UserName currUserName = it.next(); 
        lhm.put(currUserName.userName, currUserName.userPass); 
       } 
       return lhm; 
      } 
    } 

    class UserName 
    { 
     public String userName; 
     public String userPass; 

    } 

回答

1

查看getView方法。您可以使用position參數和if語句來實現此目的。

例如

public View getView (int position, View convertView, ViewGroup parent) { 
    if(position == 0) { 
     //inflate your banner image here 
    } else { 
     //infalte your textView here 
    } 
} 
+0

感謝suitianshi,但能有一種方式來獲得它使用layout.xml完成​​?還是使用getView唯一的出路? –

+0

然後你可以嘗試在你的'RelativeLayout'中添加一個'ListView'標籤。但是因爲你只想顯示橫幅一次,所以你必須爲所有不在位置0的項目調用setVisibility(View.GONE)''''''''''''''''猜測你不會想要使用這種方法(你仍然會一個'if'語句。) – suitianshi

0

嘗試這樣的:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:id="@+id/header" > 


    <TextView 
    android:id="@+id/label" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@+id/label" 
    android:textSize="50px" /> 

</RelativeLayout> 

//添加標題,列表視圖:

LayoutInflater inflater = getLayoutInflater(); 
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.header, lv, false); // This layout contains the header textview 
lv.addHeaderView(header, null, false); 
+0

嗨菲達,我編輯了我的問題與活動類。我應該把這個代碼放在我的onCreate方法中嗎? –

+0

是的,只是在聲明/定義listview之前和設置適配器之前。 – fida1989

相關問題