This issue gave me a painful headache. Basically I have a GridView
-based Gallery app. Everything is working fine. But I want to have equal spacing, that is horizontal and vertical, between the images, so it's easier to distinguish one from another.
This is my activity_main.xml
:
<?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"
tools:context="com.example.gallery.MainActivity">
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:columnWidth="80dp"
android:scrollingCache="true"
android:smoothScrollbar="true"
android:horizontalSpacing="3dp"
android:verticalSpacing="3dp"
android:padding="0dp"
android:clipToPadding="false"
android:clipChildren="true"
android:numColumns="auto_fit"
android:adjustViewBounds="true"
android:gravity="center"
android:stretchMode="columnWidth"/>
</RelativeLayout>
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private GridView gridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridView);
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getApplicationContext(), FullScreenAnimationAdapter.class);
i.putExtra("id", position);
startActivity(i);
}
});
}
}
And the ImageAdapter
's GetView
:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(ctx);
imageView.setImageResource((int)getItem(position));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(240, 240));
return imageView;
}
So I have both horizontalSpacing
and verticalSpacing
set to 3dp
. Yet my application looks like this:
All the images are 1920x1080
and 96 dpi
.
Is there something I am doing wrong? Should I change some properties in the xml file, or maybe I should use a different ScaleType
for my ImageView
s?