Monday 15 September 2014

layout_weight of LinearLayout for android doesn't work -


I'm new to Android and I'm working with linear layout. I want to put an image that takes up to 20% of the screen. These codes are but this does not work.

  & lt ;? Xml version = "1.0" encoding = "UTF-8"? & Gt; & Lt; Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" android: : Background = "@drawable / sky" & gt; & Lt; RelativeLayout Android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: layout_weight = "2" & gt; & Lt; ImageView Android: id = "@ + id / imageView1" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: src = "@drawable / school road" /> & Lt; / RelativeLayout & gt; & Lt; RelativeLayout Android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: layout_weight = "8" & gt; & Lt; / RelativeLayout & gt; & Lt; / LinearLayout & gt;   

The result is that the left layout moves up to 80% of the screen.

What's wrong with my code?

Specify the orientation of your LinearLayout

And when you use the load, then you should set the width (or height) to 0dp with the same as your orientation.

So if your orientation is vertical, you should set the height to zero while using the weight. If it is horizontal compared to the width, then the weight should be zero when using.

Try this:

  & lt ;? Xml version = "1.0" encoding = "UTF-8"? & Gt; & Lt; Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" android: : Background = "@drawable / sky" android: orientation = "vertical" & gt; & Lt; RelativeLayout Android: layout_width = "fill_parent" android: layout_height = "0dp" Android: layout_weight = "2" & gt; & Lt; ImageView Android: id = "@ + id / imageView1" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: src = "@drawable / school road" /> & Lt; / RelativeLayout & gt; & Lt; RelativeLayout Android: layout_width = "fill_parent" android: layout_height = "0dp" Android: layout_weight = "8" & gt; & Lt; / RelativeLayout & gt; & Lt; / LinearLayout & gt;    

No comments:

Post a Comment