I know the effect of setting layout_width (or height) to 0dp in combination with layout_weight in a LinearLayout, as answered in this question:
Why is 0dp considered a performance enhancement?
However, in the example code on the Android developer guide here
http://developer.android.com/guide/topics/ui/layout/relative.html
Why do they set the layout_width of the Spinner to 0dp ? So what is exactly the reason or the trick behind this ? My guess for this is the horizontal position for the Spinner can already be determined by the relative attributes, so they just specify it as 0dp for the same reason as above case. However, I can't seem to find actual documentation on this so it's still a bit confusing.
When a child view is anchored on both sides (in RelativeLayout), the width is ignored. In this case, the Spinner knows it width because of
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/times"
I've seen this trick in several projects too, and the reason is exactly the same why they do it in LinearLayout with "weight", but here the key attribute is: android:layout_toLeftOf="#+id/times" it does exactly the same trick as "weight" would do with linear layouts, so this is a way to say, take all the space remaining from android:layout_alignParentLeft="true" until android:layout_toLeftOf="#+id/times" without hardcoding any specific width...
Regards!
we use 0dp while using with layout_weight because it will neglect that attribute and it will go as per the layout_weight..
Related
This problem is better described with an example:
As you can see in the resulting rendered layout, the heights from first_view and second_view are different, and the only thing that is different is that the first_view uses
layout_width=match_parent
instead of
layout_width="0dp";
layout_constraintStart_toStartOf="parent";
layout_constraintEnd_toEndOf="parent".
Is it a bug or the expected behavior?
Taken from the official docs:
Important: MATCH_PARENT is not recommended for widgets contained in a
ConstraintLayout. Similar behavior can be defined by using
MATCH_CONSTRAINT with the corresponding left/right or top/bottom
constraints being set to "parent".
and official training
Note: You cannot use match_parent for any view in a ConstraintLayout.
Instead use "match constraints" (0dp).
That being said, your example is also not valid because you are using 0dp (match constraints) for height without specifying the bottom constraint which might lead to unexpected behaviour of the view. To match constraints for a dimension you need to declare both ends.
When using Android ConstraintLayout, should I favour constraints over size properties (layout_width, layout_height) or the other way around? I'm asking mostly from a performance point of view but any comments are appreciated.
As an example, let's say I have
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
Then I could get rid of both layout_constraintLeft_toLeftOf and layout_constraintRight_toRightOf, or leave them and change layout_width to 0dp and have the same result.
Of course I would remove any unused constraint or property to make code cleaner and easier to understand, but does it have any other drawbacks I'm missing?
Read ConstraintLayout & Build a Responsive UI with ConstraintLayout.
MATCH_PARENT is not recommended for widgets contained in a
ConstraintLayout. Similar behavior can be defined by using
MATCH_CONSTRAINT with the corresponding left/right or top/bottom
constraints being set to "parent".
Using 0dp, which is the equivalent of MATCH_CONSTRAINT (The view expands as much as possible to meet the constraints on each side)
You can also use ratio if both dimensions are set to MATCH_CONSTRAINT (0dp). In this case the system sets the largest dimensions that satisfies all constraints and maintains the aspect ratio specified.
I have the following requirement in Android:
Given a horizontal LinearLayout, I have five buttons in it. All the buttons should have equal width and height. The height of each button is same as their parent LinearLayout and spacing between them should remain constant. The height of LinearLayout is not constant and depends on form factor and other layouts sizing. Therefore, I can not assign fixed with/height to each button.
While I can easily achieve that very easily in iOS with the help of constraints, I am not sure how to achieve this in Android at design time. Is there some way to achieve this or is it possible programatically only?
The height of each button is same as their parent LinearLayout
Set the height to match_parent
For your width, you'll have to calculate the screen size programmatically and set the widths accordingly. See this question.
Try this . As you have a horizontal LinearLayout with 5 buttons, for equal spacing of all buttons, you must first allocate the space to each button of of the total space available like this..
<LinearLayout
android:weightSum="5"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="allocate it programatically"
/>
This button shown above has to be for all 5 buttons u have. The idea allocating weight sum of 5 to LinearLayout and then dividing it as 1 to each button. Note the width here for the button should be 0dp.
Then dynamically change your button Height and Width per your requirement
Unfortunately no typical layout in Android seems to have the concept of this kind of layout constraint (square like elements).
It's the layout that ultimately determines the sizes of their children. Therefore if you want that you have to write your own layout implementing this constraint.
For this extend ViewGroup and in onLayout enforce that width of the children or a specific children equals height. You could even invent your own LayoutParams for this task. See the documentation of ViewGroupfor a general example. The exact implementation very much depends on which other requirements you have for your layout.
Can I using xml layout attributes put image to the following position? Or I must calculate position on src?
http://i.stack.imgur.com/gox4d.png
You can do it using the XML attributes.
Just use follwing attribute in your ImageView:
Android:layout_marginTop="20dip"
Android:layout_height="20dip"
Only use DIP as these are density independent Pixels
dont use PX.
Yes, you can, for the simple example you posted I would use a vertical LinearLayout, with a FrameLayouts to 'fill' the blank space. Set the LinearLayout weightSum to 1, then set the first FrameLayout weight attribute to 0.2 (1/5), and imageview to 0.2. Also set both the frame layout and imageview layout_height values to 0px.
While this solution works, I'm sure there is a better and cleaner way out there. Hopefully someone will post it.
You can use AbsoluteLayout, calculate exact coordinates based on screen size and orientation and then position the image with absolute coordinates.
You can just use weights for different containers in a vertical linear layout.
Why does gravity offer a "fill_vertical" option? How is this any different from setting layout_height "fill_parent"? What if I choose a fixed layout_height and "fill_vertical" for gravity? Won't this contradict?
I believe that using gravity="fill_vertical" indicates that the View should take up as much space as possible vertically while still respecting other Views' height constraints. If you use layout_height="fill_parent", you're telling the View to take up the entire parent's height; this may result in a single View pushing other Views off the viewport.