There was one particular issue that I ran into that is worth noting, for its solution was not immediately obvious. I was making a two-column GridLayout, where an image was on the left and text was on the right. Here's a simplified version of this layout:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" > <Space android:layout_width="100dp" /> <TextView android:text="@string/two_cities" /> </GridLayout>
The two Views would align next to each other nicely, but the TextView on the right would end up overflowing off the edge (the example below is not cropped - this is how it appears on the device):
What was happening was that the TextView would take up the entire width of the GridLayout, instead of taking up just the width of the cell (as I expected).
It turns out the solution is to set the width of the TextView to zero, then let the View fill its space dynamically using layout_gravity:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" > <Space android:layout_width="100dp" /> <TextView android:layout_width="0dip" android:layout_gravity="fill_horizontal" android:text="@string/two_cities" /> </GridLayout>
Now the problem is solved:
You need both of the highlighted attributes above; one or the other won't fix the problem. However, it will nicely solve the issue and should be kept in mind anytime you setup a GridLayout child to fill horizontally or vertically.
I was missing the android:layout_width="0dp". Not sure why that is necessary, but it certainly worked well.ReplyDelete
I believe the reason this is necessary is because the rendering happens in two steps:Delete
1. Determine the width of the view based on layout_width.
2. Adjust the width based on layout_gravity.
If the width is already set to a large size due to step #1, it won't reduce it in step #2. But if the width starts small, it will expand due to layout_gravity.
I think you meantReplyDelete
Thanks a lot. You ended my headache :-)ReplyDelete
Thanks, you solved my problem.ReplyDelete
Thank you for posting this! Just spent hours trying to figure out wtf was going on until I found this and problem solved :)ReplyDelete
thanks a lot for this, saved me a couple of hrs.ReplyDelete
Not a lot of working answer on internet . ThanksReplyDelete
Thanks for sharing this! It was super helpful.ReplyDelete
hey, i am having an issue with this trick. If the text is in the right most cell then this works well but it doesn't work when the text is in the left most cell which I want to scale dynamically and the cell on the right is fixed.ReplyDelete
Ditto to what others have said - this was SUPER helpful. Been pulling my hair out trying to figure out why and you solved it for me. Thanks SO much!!!ReplyDelete
Omg, thank you so much. Have been struggled with this for all day :'(ReplyDelete
Prologics, India offers HDR image blending, real estate photo editing and image editing services, image clipping, background removal, photo clipping path service, photo editing , color correction, photo retouching & restoration, clipping path, cropping, resizing services, 3d floor design, 2d floor plan services & many more.ReplyDelete
You are a real scholar, sir. Thank you so much.ReplyDelete
Nhung Chuyen Di Cuoc DoiReplyDelete
Du Lich Tu Tuc
Kien Thuc Du Lich
Book Ve Du Lich Gia Re