My problem is that some of my gridview items have a different height than others. I want that all items have the same width and height. Here is the code for the item layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container_categoria"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
android:gravity="center"
android:background="#232323"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView5"
android:layout_width="75dp"
android:layout_height="75dp"
android:paddingBottom="5dp"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/category_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Centro de Investigação de Exemplo" />
</LinearLayout>
And here is my main layout file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent" >
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit"
android:verticalSpacing="5dp"
android:horizontalSpacing="5dp"
android:id="@+id/gridView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
And here is an image showing the problem: image with problem
It is happens because your TextView, has different text length, try this easy way (in your item xml):
<TextView
android:id="@+id/category_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:maxLines="1"
android:text="Centro de Investigação de Exemplo" />
Or use fixed height of GridView items like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container_categoria"
android:layout_width="match_parent"
android:layout_height="300dip"
android:padding="20dp"
android:gravity="center"
android:background="#232323"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView5"
android:layout_width="75dp"
android:layout_height="75dp"
android:paddingBottom="5dp"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/category_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Centro de Investigação de Exemplo" />
</LinearLayout>
You may calculate height programatically in your code.
This is because of your TextView.
As far as I can see, where you have 3 lines of text the item is bigger, and where you have only 2 the item is smaller.
The obvious choice here would be to change the LinearLayout of the item to a RelativeLayout and scale the boxes accordingly. Or you can set a fixed Height to your layout.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.