【问题标题】:How to arrange imageview size and play sound on clicking the image in gridview? [duplicate]如何在gridview中单击图像时排列imageview大小和播放声音? [复制]
【发布时间】:2015-05-01 05:55:56
【问题描述】:

您好,我一直在研究 android GridView 我无法以图片的形式排列我的图像视图 Like this

在 GridView 上工作后,我只能排列我的图像Like this

无法理解如何做到这一点,而且我想在单击每个按钮后播放不同的声音..

这里是gridview.xml的代码

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

这是主要活动代码

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid_layout);

        GridView gridView = (GridView) findViewById(R.id.grid_view);

        // Instance of ImageAdapter Class
        gridView.setAdapter(new ImageAdapter(this));
    }
}

这是ImageAdapter代码

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    // Keep all Images in array
    public Integer[] mThumbIds = {
        R.drawable.alif, R.drawable.baa,
        R.drawable.taa, R.drawable.saa,
        R.drawable.raa, R.drawable.jeem,
        R.drawable.zaal, R.drawable.haa,
        R.drawable.zouen, R.drawable.haah,
        R.drawable.zowad, R.drawable.hamza,
        R.drawable.yaa, R.drawable.daal,
        R.drawable.yay
};

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

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

@Override
public Object getItem(int position) {
    return mThumbIds[position];
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ImageView imageView = new ImageView(mContext);

    imageView.setImageResource(mThumbIds[position]);
    imageView.setScaleType(ImageView.ScaleType.);
    imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
    return imageView;
}

【问题讨论】:

    标签: android android-layout android-imageview android-gridview


    【解决方案1】:

    首先使用此代码创建新的 item.xml 文件:

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">
                    <ImageView
                        android:layout_width="100dp"
                        android:layout_height="100dp"
                        android:padding="1dp"
                        android:id="@+id/itemimg"
                        android:layout_gravity="center"
                         />
                     </LinearLayout>
    

    然后请像这样更改您的适配器类 getview 方法:

    public View getView(int position, View convertView, ViewGroup parent) {
            View itemView = null;
            LayoutInflater inflater;
            final ViewHolder holder = new ViewHolder();
            if (convertView == null) {
                inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        itemView = inflater.inflate(R.layout.item, null,false); 
                    } else {
                itemView = convertView;
        }
        holder.img = (ImageView) itemView.findViewById(R.id.itemimg);
            holder.img.setImageResource(mThumbIds[position]);       
            return itemView;
    
        }
    static class ViewHolder {
            ImageView img;
                }
    

    希望有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-22
      • 1970-01-01
      • 2016-01-17
      • 2014-12-19
      • 2013-04-30
      • 2012-10-29
      • 1970-01-01
      相关资源
      最近更新 更多