我想在列表视图的顶部动态设置横幅(图像)。我正在从 web 服务调用列表视图数据。我在我的惰性适配器类中进行了此编码,但没有可见的横幅。我想像这样设置横幅。
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);*/
我的drawable中有横幅图像。
这是我的懒惰适配器类的完整代码
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
}
}