问题描述:

I'm developing an Android application I have to implement a custom layout that is composed from 6 images.

I want display a custom and full responsive layout with 2 columns and 3 row, i have implemented my code but don't work properly.

This is my source code:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

app:layout_behavior="@string/appbar_scrolling_view_behavior"

tools:context="am.pacrocebianca.MainActivity"

tools:showIn="@layout/activity_main">

<GridView xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/gridview"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:numColumns="2"

android:verticalSpacing="2dp"

android:horizontalSpacing="4dp"

android:stretchMode="columnWidth"/>

</RelativeLayout>

And the class where I create the gridview:

GridView gridview = (GridView) findViewById(R.id.gridview);

gridview.setAdapter(new ImageAdapter(this));

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;

imageView = new ImageView(mContext);

imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

if (convertView == null) {

DisplayMetrics dm = new DisplayMetrics();

getWindowManager().getDefaultDisplay().getMetrics(dm);

int width = dm.widthPixels/2;

imageView.setLayoutParams(new GridView.LayoutParams(width, width));

} else {

imageView = (ImageView) convertView;

}

imageView.setImageResource(mThumbIds[position]);

return imageView;

}

// references to our images

private Integer[] mThumbIds = {

R.drawable.comunicazioni, R.drawable.invia,

R.drawable.profilo, R.drawable.contatti,

R.drawable.turni, R.drawable.utility

};

}

}

The images are shown as I wish , but I wish it could be shown on one screen, without using the scroll, how i can do this ?

相关阅读:
Top