我想动态地在列表视图的顶部设置横幅(图像)。我正在调用来自webservices的列表视图数据.i在我的懒惰适配器类中进行了编码,但没有可见的横幅.i wana设置了这样的横幅。{ {0}}
RelativeLayout layout1 = new RelativeLayout(mContext);
ImageView image = new ImageView(mContext);
LayoutParams params2 = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
image.setLayoutParams(params2);
image.setScaleType(ImageView.ScaleType.CENTER);
image.setMaxHeight(50);
image.setMaxWidth(50);
image.getPaddingTop();
image.setImageResource(R.drawable.topheader);
layout1.getPaddingTop();
layout1.addView(image);*/
我的抽奖中有横幅图片。
这是我的lazy adpter类的完整代码
package com.example.jsonparsing;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.RelativeLayout.LayoutParams;
public class LazyAdapter extends BaseAdapter {
int counter = 0;
Context mContext;
Activity a;
public LazyAdapter(Context context) {
this.mContext = context;
this.a = a;
}
public int getCount() {
System.out.println("Called..."+ Constants.vctrCategoryId.size());
return Constants.vctrCategoryId.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println("Exception before..");
String strUrl = Constants.vctrImagePath.elementAt(counter).toString();// getting url of the images
System.out.println("Urls...." + strUrl);
URL url =null;
try {
url = new URL(strUrl);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bitmap bitmap = Constants.DownloadImage(strUrl);
Bitmap resized = Bitmap.createScaledBitmap(bitmap, 100, 100, true);
//text.setImageBitmap(bitmap);
/* this coding i did for setting banner
/*RelativeLayout layout1 = new RelativeLayout(mContext);
ImageView image = new ImageView(mContext);
LayoutParams params2 = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
image.setLayoutParams(params2);
image.setScaleType(ImageView.ScaleType.CENTER);
image.setMaxHeight(50);
image.setMaxWidth(50);
image.getPaddingTop();
image.setImageResource(R.drawable.topheader);
layout1.getPaddingTop();
layout1.addView(image);*/
RelativeLayout layout = new RelativeLayout(mContext);
//RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
//layout.setLayoutParams(params);
TextView text1 = new TextView(mContext);
text1.setText(Constants.vctrCategory.elementAt(counter).toString());
LayoutParams params1 = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
text1.setLayoutParams(params1);
text1.setTextSize(20);
text1.setGravity(Gravity.RIGHT);
layout.addView(text1);
ImageView img = new ImageView(mContext);
img.setImageBitmap(resized);
layout.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
layout.addView(img);
counter++;
return layout;
}
private void setContentView(ImageView image) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:2)
从布局的角度来看你的解决方案:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/imageView1" >
</ListView>
</RelativeLayout>
猜猜它有帮助!
答案 1 :(得分:1)
您是否尝试过addHeaderView()类
的listView方法答案 2 :(得分:0)
使用此:
View headerView1 = ((LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom_add, null, false);///this is your imageview
list_view.addHeaderView(headerView1);
答案 3 :(得分:0)
@ kittu88的完美答案。如果你想动态添加它,你可以试试这个。
XML:
<LinearLayout
android:od="@+id/banerLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
Java代码
LinearLayout bLayout = (LinearLayout) findViewbyId(R.id.banerLayout);
bLayout.addView(YOUR Image);
答案 4 :(得分:0)
您可以使用以下代码,
list_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="none"/>
<TextView
android:id="@+id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/empty_list" />
</LinearLayout>
list_header.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/header_layout_root"
android:background="#ffffff"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:background="#000000"
android:textColor="#ffffff"
android:textSize="28dp"
android:gravity="center_horizontal"
android:paddingTop="10dp"
android:paddingBottom="10dp">
</TextView>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/sample_image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/sample_image"
android:scaleType="fitXY">
</ImageView>
<TextView
android:id="@+id/sample_title"
android:text="@string/sample_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffffff"
android:textSize="12dp"
android:background="#AA000000"
android:padding="5dp"
android:layout_alignParentLeft="true"
android:layout_alignBottom="@id/sample_image">
</TextView>
</RelativeLayout>
</LinearLayout>
接下来回到ListActivity类,是时候放入代码将标题添加到列表中,将它放在调用setContentView()的下方:
ListView lv = getListView();
LayoutInflater inflater = getLayoutInflater();
View header = inflater.inflate(R.layout.list_header, (ViewGroup) findViewById(R.id.header_layout_root));
lv.addHeaderView(header, null, false);
ListWithHeader.java
import android.app.ListActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ListWithHeader extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_main);
//add header to list
ListView lv = getListView();
LayoutInflater inflater = getLayoutInflater();
View header = inflater.inflate(R.layout.list_header, (ViewGroup) findViewById(R.id.header_layout_root));
lv.addHeaderView(header, null, false);
//add some list items
String listItems[] = {"List Item One", "List Item Two", "List Item Three", "List Item Four", "List Item Five"};
lv.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, listItems));
}
}