将单个图像放在多个位置而不创建imageview

时间:2012-02-20 11:49:08

标签: android image imageview android-imageview

大家好我需要在多个地方放置一张图片。

假设我需要在布局中的5个位置放置一个图像,但只使用一个<ImageView> id

<ImageView
        android:id="@+id/img"
        android:layout_width="340dip"
        android:layout_height="240dip"
        android:layout_marginBottom="60dip" />

如何实现这一点请帮忙。

3 个答案:

答案 0 :(得分:0)

使用此代码使用网格视图:

Main.java:

package sra.gri.vie;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this));

        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Toast.makeText(Main.this, "" + position, Toast.LENGTH_SHORT).show();
            }
        });
    }


    public class ImageAdapter extends BaseAdapter {
        private Context mContext;

        public ImageAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            return mThumbIds.length;
        }

        public Object getItem(int position) {
            return null;
        }

        public long getItemId(int position) {
            return 0;
        }

        // create a new ImageView for each item referenced by the Adapter
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {  // if it's not recycled, initialize some attributes
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8, 8, 8, 8);
            } else {
                imageView = (ImageView) convertView;
            }

            imageView.setImageResource(mThumbIds[position]);
            return imageView;
        }

        // references to our images
        private Integer[] mThumbIds = {
                R.drawable.icon, R.drawable.icon,
                R.drawable.icon, R.drawable.icon,
                R.drawable.icon, 

        };
    }

}

main.xml://布局

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
/>

拉我知道这篇文章是否有任何帮助:)

答案 1 :(得分:0)

你可以这样做

RelativeLayout rl = (RelativeLayout) findViewById(R.id.my_relative_layout); 
ImageView iv = new ImageView(this);  
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = 50; params.topMargin = 60; 
rl.addView(iv, params);

然后在你的主视图中添加这个r1然后再次更改边距,高度和其他参数并再次添加,就像你没有时间一样

答案 2 :(得分:0)

你试试包含标签吗?

img.xml

 <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:src="@drawable/ic_home"/>

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="fill_parent"
    android:orientation="vertical" >
    <include layout="@layout/img"/>
    <include layout="@layout/img"/>
    <include layout="@layout/img"/>
    <include layout="@layout/img"/>
</LinearLayout