I have this layout file from Chapter 13 of Android Programming: The Big Nerd Ranch Guide:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:src="#drawable/armstrong_on_moon"
android:contentDescription="#string/hellomoon_description"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerInside"
android:layout_weight="1"/>
<TableRow
android:gravity="center|bottom"
android:layout_weight="0">
<Button
android:id="#+id/hellomoon_playButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hellomoon_play"/>
<Button
android:id="#+id/hellomoon_stopButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hellomoon_stop"/>
</TableRow>
</TableLayout>
And it results in a layout that looks like this. What I do not understand is how the TableRow shows up in the layout at all since its layout_weight is 0 and the ImageView's layout_weight is 1. My understanding is that the way layout_weight works is it will first respect the layout_width and layout_height of each element, and then divide up the remaning space between the elements based on layout_weight. But since the ImageView has match_parent for both its layout_width and layout_height, shouldn't it take up the whole screen and then leave nothing left to divide with the TableRow? I believe that the TableRow also has match_parent for both its layout_width and layout_height attributes since it inherited them from TableLayout, but since it came second I thought the ImageView would dominate the screen. Also, even if the the ImageView shouldn't take up the whole screen, aren't the layout_weights saying that the ImageView should get 100% of the remaining space and the TableRow should get 0%? Additionally, when I increase the layout_weight of the TableRow, the TableRow just gets pushed further and further off the screen as it increases more-- but I thought that this should increase the space the TableRow takes up on the screen. Can anyone explain what I'm misunderstanding here.
In case it helps, my understanding for layout_weight comes from this example in the book:
**How android:layout_weight works**
...
LinearLayout makes two passes to set the width of a view. In the first pass, LinearLayout looks at layout_width (or layout_height, for vertical orientation). The value for layout_width for both the Button and CheckBox is now wrap_content, so each view will get only enough space to draw itself (Figure 8.12).
...
In the next pass, LinearLayout allocates any extra space based on the values for layout_weight.
The TableRow has a height of wrap_content. This is enforced by the TableLayout, as per the documentation:
The children of a TableLayout cannot specify the layout_width attribute. Width is always MATCH_PARENT. However, the layout_height attribute can be defined by a child; default value is WRAP_CONTENT. If the child is a TableRow, then the height is always WRAP_CONTENT.
TableLayout and TableRow are both LinearLayout subclasses, but I experienced the same strange behaviors when I created a layout like the one in your post. Browsing the source of TableLayout and TableRow, it's not immediately clear to me what causes this (clearly there's some overridden logic in the measuring code, but that's not really satisfactory to figure out things like column spans and to adjust column widths and such, but I'd have to dig more to figure out what's going on).
Unless this example actually needs/uses some of the special behaviors of TableLayout and TableRow, I would advise just using LinearLayouts instead for the sake of clarity. The presence of layout_weight with those two views is frankly confusing and clearly doesn't behave as expected. In either case, you don't even need the layout_weight="0", whether you use twoLinearLayout`s or keep the example as-is.
Related
I am a newbie in Android-programing and currently I'm building my first application. I have a LinearLayout (with several imageviews), which is situated inside of basic RelativeLayout.
The xml width and heigth settings of LinearLayout are as follows:
android:layout_width="135dp"
android:layout_height="match_parent"
The settings of imageviews inside LinearLayout look like this:
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="125dp"
android:layout_weight="3"
.....
When I start my application on small screens, it looks normal, but when the screen is big, all the pictures become very narrow (the width of LinearLayout inside RelativeLayout stays the same on small and big screen). I tried to set it in percentage, but that is not allowed. So I ask you: how can I set the width of LinearLayout to be like 25-30% of the width of parent RelativeLayout(to make my images look pretty on any device).
I know that there is a layout_weight attribute, but it doesn't seem to work with layouts inside another layouts (or maybe I have no idea what is correct or what is not).
As I know, you cant take parent layout percantage to child layout.
Linearlayout add elements to as columns or as row.
linearlayout api
Relative Layout
A Layout where the positions of the children can be described in
relation to each other or to the parent.
Relative Layout api
Why do you use linearlayout inside relative layout? In linearlayout you cant design your layout elements puting element with mouse,But in relativelayout you can.
I'm doing a menu. The problem is that my buttons are too close to one another. I would like to separate them a bit.
Also I would like to extend them (rozszerzyć je) to the similar sizes.
Here you have the code:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button2"
android:layout_centerHorizontal="true"
Here you have the image of this problem:
And each button out of the 4 ones has a similar code. (the difference is in layout_above.
How to make it?
Thanks in advance!
Any reason why you have to use a RelativeLayout? If you can use a LinearLayout I'd use something like:
<LinearLayout
android:layout_width="..."
android:layout_height="..."
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:padding_top="4dp"
android:padding_bottom="4dp"
... />
</LinearLayout>
This would get all your buttons to be the same size horizontal and vertical. You can tweak the padding top and bottom to get the desired effect.
The way layout_weight works is it takes the leftover space of the parent view, and portions them out to the children views according to their weights. Since the height of every button is 0dp, 100% of the vertical space is left to partition out. Since the weight of all the buttons is the same, they will be roughly the same size.
This explanation is for a vertical LinearLayout. For a horizontal, just switch the values of layout_height and layout_width.
I have a table layout with 5rows. I'd like the size of first row to take the rest of the screen while the remaining rows take as much space they need based on the content inside.
My first intuition was to make the first row's layout width and height set to match_parent while having the other rows' width to match_parent and height to wrap_content. Needless to say, this does not work.
How can I accomplish this?
I'd like the size of first row to take the rest of the screen while
the remaining rows take as much space they need based on the content
inside.
I think that using android:layout_weight="1" on the first TableRow should solve the problem you have.
Put your TableLayout inside a ScrollView and fix the Height of your First TableRow which will solve your issue. Hope it works.
Seeing as TableLayout inherits from LinearLayout, have you tried setting Row height to 0dp and setting layout weights for each one to achieve the desired effect? Like so:
<TableRow
android:layout_height="0dp"
android:layout_weight="2"
android:layout_width="match_parent">
<TableRow
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent">
and so on....
stick to your first intuition but add layout_weight="1" to the first row.`
How to remove this warning?
Use a layout_height of 0dip instead of wrap_content for better performance
Ok let me explain through an example,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hello World!!" />
</LinearLayout>
In the above example I am using android:layout_weight="1" for TextView by which I am telling the layout that my TextView will take the full height as of parent layout(LinearLayout). So, in that case android:layout_height="wrap_content" is of no use as the TextView will have the full size as parent LinearLayout. So, in that case its better to add android:layout_height="0dp" to make TextView wrap itself to the height of parent Layout.
You must have use layout_weight attribute in child element which is present in vertical parent layout.So in that case you have already told parent how much height is to be applied to child.
So in child element replace
android:layout_height="wrap_content"
with
android:layout_height="0dp"
However, this is just a guess and you should post your entire layout XML
If you have any value specified for layout_weight, even if it is set to 0 it will throw this warning.
The problem is, that setting height/width it to 0dp, in this case results in the element not being shown at all. Just remove the layout_weight parameter and all is well.
Of course as many others said, this is not a problem if you have a layout_weight greater than zero specified. Just set layout_height or layout_width (depending on the layout) to 0dp.
I just ran into this problem, so...
Make sure parent Layout's orientation is set matching the direction you want the weightSum to correspond.
In other words, make sure the parent Layout's orientation is set to "horizontal" if it is the layout_width of a child View that you want to use weightSum with.
Or alternatively:
Set the parent's Layout orientation to vertical if it is the childs layout_height attribute you want to use weightSum with.
I have ads layout under the ScrollView. In order to prevent the ScrollView overlapping ads layout, I have to use
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" // <--- THIS!
>
//...scrollview content
</ScrollView>
<include layout="#layout/ads_468x60"/>
What is the meaning of layout_weight if both width and height have been set to fill_parent?
According to documentation, this should not work, or to be precise, if both layout_height and layout_width have been set other than 0dp, then layout_weight is disregarded. But, in this example it works and bottom ads layout will not be shown without the attribute android:layout_weight="1" inside of the ScrollView.
use android:layout_height="0dp"
android:layout_weight="1" tells layout manager to fill all the free space.
In your case, the layout_weight makes it ok to make your view smaller than defined (fill_parent) and it is shrinked to fit the screen space available. The weight indicates how to change the sizes of one or more components relatively to each other. In your case, you have just one view with weight. It is considered faster to use 0dp, as the system needs to measure less.
layout_weight is used for situations when you want to decide dynamically the width or height, so when you say layout_height = "0dp".
It actually now depends on layout_weight attribute to make correct decision for it.
So by setting up weightsum property on parent layout you can accordingly distribute weights on child layouts by providing layout_weight.
refer this guy's answer and description on layout_weight :
Android Layout Weight