我正嘗試在列表視圖的頂部使用我的產品標題PNG創建一個簡單的佈局。像我很喜歡這個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。 我們如何將圖像橫幅放在列表視圖的頂部?
這是我的主要活動。我正在嘗試創建登錄頁面。
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;
}
感謝suitianshi,但能有一種方式來獲得它使用layout.xml完成?還是使用getView唯一的出路? –
然後你可以嘗試在你的'RelativeLayout'中添加一個'ListView'標籤。但是因爲你只想顯示橫幅一次,所以你必須爲所有不在位置0的項目調用setVisibility(View.GONE)''''''''''''''''猜測你不會想要使用這種方法(你仍然會一個'if'語句。) – suitianshi