0

我有一个ListView在其中列出的壁纸。我想打开大图onClick并将List item其设置为墙纸。怎么做?这是我的代码:

abc.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/logo"
        android:layout_width="50px"
        android:layout_height="50px"
        android:layout_marginLeft="5px"
        android:layout_marginRight="20px"
        android:layout_marginTop="5px"
        android:src="@drawable/aa" >
    </ImageView>

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="22px" >
    </TextView>

</LinearLayout>

abc1.java

package com.example.wallpaper;
import com.mkyong.android.adaptor.MobileArrayAdapter;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;
import android.view.View;

public class abc1 extends ListActivity {

    static final String[] WALLPAPER_IMG = 
               new String[] { "Wallpaper1", "Wallpaper2", "Wallpaper3", "Wallpaper4","Wallpaper5","Wallpaper6","Wallpaper7","Wallpaper8"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setListAdapter(new MobileArrayAdapter(this, WALLPAPER_IMG));

    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
            //get selected items
            String selectedValue = (String) getListAdapter().getItem(position);
            Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();

    }

}

MobileArrayAdapter.java

package com.mkyong.android.adaptor;

import com.example.wallpaper.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MobileArrayAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final String[] values;

    public MobileArrayAdapter(Context context, String[] values) {
        super(context, R.layout.abc, values);
        this.context = context;
        this.values = values;
    }
    int onscreen;
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.abc, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.label);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
        textView.setText(values[position]);

        // Change icon based on name
        String s = values[position];

        System.out.println(s);

        if (s.equals("Wallpaper1")) {
            imageView.setImageResource(R.drawable.a);
            onscreen = R.drawable.a;
        } else if (s.equals("Wallpaper2")) {
            imageView.setImageResource(R.drawable.b);
            onscreen = R.drawable.b;
        } else if (s.equals("Wallpaper3")) {
            imageView.setImageResource(R.drawable.c);
            onscreen = R.drawable.c;
        } else if (s.equals("Wallpaper4")) {
            imageView.setImageResource(R.drawable.d);
            onscreen = R.drawable.d;
        }else if (s.equals("Wallpaper5")) {
            imageView.setImageResource(R.drawable.e);
            onscreen = R.drawable.e;
        }else if (s.equals("Wallpaper6")) {
            imageView.setImageResource(R.drawable.f);
            onscreen = R.drawable.f;
        }else if (s.equals("Wallpaper7")) {
            imageView.setImageResource(R.drawable.g);
            onscreen = R.drawable.g;
        }else{

            imageView.setImageResource(R.drawable.h);
            onscreen = R.drawable.h;
        }

        return rowView;
    }


}
4

1 回答 1

1

你可以通过几种方式来解决这个问题。

1)使用android原生图库打开图像并通过那里可用的隐式功能将图像设置为壁纸(通过选择菜单并将其设置为壁纸)。为此,您必须发送此处描述的意图。

2)另一种方法是创建自己的imageView来显示图像,然后将图像设置为壁纸。您可以按照此处所述设置壁纸。

于 2012-09-07T09:06:34.307 回答