0

我想在列表视图的顶部动态设置横幅(图像)。我正在从 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

}

}
4

5 回答 5

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>

猜猜有帮助!

于 2012-11-22T07:40:25.313 回答
1

您是否尝试过listView类的addHeaderView() 方法

于 2012-11-22T07:40:53.520 回答
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);
于 2012-11-22T07:42:46.207 回答
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);
于 2012-11-22T07:43:57.450 回答
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));
    }
}
于 2012-11-22T07:50:46.943 回答