1

I use a LinearLayout (with some content) as the view for my cells in a GridView. The problem is that although I have specified the height of the layout (android:layout_height="50pt") There are some cells that are bigger. The layout is vertical and the items inside have no height(0pt), but they have weight, there are 2 TextViews and 1 ImageView inside. I noticed that the image inside the cells that cause the problem is a bit bigger, but still why would the view grow? I would appreciate your help.

XML:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="50pt"
android:gravity="center"
android:orientation="vertical">

<ImageView
    android:id="@+id/ivHero"
    android:layout_width="match_parent"
    android:layout_height="0pt"
    android:layout_weight="4"
    android:background="@color/white"
    app:srcCompat="@drawable/avatar" />

<TextView
    android:id="@+id/tvName"
    android:layout_width="match_parent"
    android:layout_height="0pt"
    android:layout_weight="1"
    android:gravity="center"
    android:singleLine="true"
    android:text="TextView"
    android:textColor="@color/white"
    android:textSize="7pt" />

<TextView
    android:id="@+id/tvPercent"
    android:layout_width="match_parent"
    android:layout_height="0pt"
    android:layout_weight="1"
    android:gravity="center"
    android:text="20%"
    android:textColor="@color/white"
    android:textSize="7pt" />

Dinu Nicolae
  • 1,019
  • 2
  • 17
  • 42

3 Answers3

1

Could you try to use dp instead of pt in your layout? I tested it using dp and pt with different image sizes and it looks different.

Check this out as well

pt - A point, a common font size unit, on the screen. This is a density independent unit, and the physical size of a single pt is the same on every screen density. There are 72 pt in an inch. The number of pixels a single pt translates to varies depending on screen density.

dp - A density independent pixel. This is a density independent unit, however the physical size of a single dp is only approximately the same on every screen density. There are approximately 160 dp in an inch. A scaling factor, depending on the density bucket of the device, is applied to convert dp to the number of pixels at 160 dpi. The number of pixels a single dp translates to varies depending on the pixel on screen density and the density bucket the device falls into.

sp - A scale independent pixel, specially designated for text sizes. This is a density independent unit, however the physical size of a single sp is only approximately the same on every screen density. Scaling factors, depending on the density bucket of the device, as well as the user’s text size preference, are applied to convert sp to the number of pixels at 160 dpi. The number of pixels this translates to varies depending on screen density and the density bucket the device falls into.

for more detailed info you could check this other answer in StackOverflow or on this github repo

Ruan_Lopes
  • 1,381
  • 13
  • 18
1
  1. Here, you have used "layout_weight" param for all three elements and provide "layout_height" is 0dp. Its wrong.
  2. Whenever you using layout_weight param you should provide "layout_height = match_parent" and "layout_width = match_parent".
  3. Just change layout_height = match_parent and run the app, it will working fine.

Thank you

1

I found out that I was inflating my view the wrong way and this was the reason why the xml was ignored. This article describes my problem: https://possiblemobile.com/2013/05/layout-inflation-as-intended/

I was doing it this way:

view = p1 ?: LayoutInflater.from(context).inflate(R.layout.ticket_grid,null)

with a null root, just add the root:

override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
view = p1 ?: LayoutInflater.from(context).inflate(R.layout.ticket_grid, p2, false)
}
Dinu Nicolae
  • 1,019
  • 2
  • 17
  • 42