简体   繁体   中英

How can we place an image banner at the top of a list view in android?

I am trying to create a simple layout with my Product banner png at the top of a list view. Like 在此处输入图片说明 I am ausing this 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> 

I tried placing this banner in a linear layout above the relative layout but it is not permitted and i seeing this UI. 在此处输入图片说明 How can we place an image banner at the top of a list view?

This is my Main Activity. I am trying to make a login page.

        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;

    }

See the getView method . You can use the position parameter and a if statement to achieve this.

For example

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

Try like this:

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

// Add header to 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM