我们如何在Android的列表视图顶部放置图像横幅?

时间:2014-01-26 07:15:36

标签: android android-layout textview

我正在尝试使用列表视图顶部的Product banner png创建一个简单的布局。和enter image description here一样,我正在使用这个layout.xml。

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

    }

2 个答案:

答案 0 :(得分: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
     }
 }

答案 1 :(得分: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>

//将标题添加到listview:

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