I have created a custom layout manager derived from LinearLayout. (I do not really use much of LinearLayout functionality but I do not want to care about all of many ViewGroup abstract methods.) I have overwritten only
protected void onLayout(boolean changed, int l, int t, int r, int b)
method and I call child.layout(...) in MyLayout.onLayout(...) for each of its children. All other stuff is original LinearLayout. It will work fine and children will be placed correctly in their parent MyLayout if the children are simple controlls without children (grandchildren of the MyLayout).
Problems come when I use some ViewGroup (LinearLayout, ViewSwitcher, ...) with children as child of MyLayout. In this case children (of MyLayout) are placed well but grandchildren (of MyLayout) inside of each child not. It seems paramaters and values like wrap_content, fill_parent, layoutWeight are not handled correctly. For example ViewSwitcher in MyLayout does not show its content, LinearLayouts (child of MyLayout) children (grandchildren of MyLayout) with fill_parent have size of 0px, CompoundButton (child of MyLayout) sometimes does not show its text...
It seems child.layout(...) in MyLayout.onLayout sets the size and position of each child correctly but I need to call some other method to make layout in each child working well (if the child is ViewGroup with children). Is there something like that or MyLayout with just changed onLayout should work well?
Thanks for all advices.
sounds like you need to overide onMeasure and call measure() on each of the children. if you children are viewgroups and the grandchildren are views then you want to loop through the children and call measure(int,int). then the children should call measure on the grandchildren.
take a look at the linearLayout source:
http://www.grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.3.6_r1/android/widget/LinearLayout.java#LinearLayout.onMeasure%28int%2Cint%29
Related
From the developer guide it says that
though a view can define a padding, it does not provide any support for margins. However, view groups provide such a support.
but why i can set layout_margin attributes in ImageView,EditView and so on,they exist and work just like padding,
I can't understand what the guide says, Can someone help me to understand it?
Basically that means that margins are defined in xml for child views, but are used by their parent.
Technically, paddings are fields of the View class. Paddings are being used in the View.draw() method by a View itself. See:
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.0.2_r1/android/view/View.java#15156
Margins are fields of the MarginLayoutParams class. Margins are used by a ViewGroup to layout its children. See:
http://developer.android.com/reference/android/view/ViewGroup.MarginLayoutParams.html
EDIT:
Margins are loaded to MarginLayoutParams and then used in the layouting phase.
Method which uses these xml attributes to create MarginLayoutParams in FrameLayout:
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.0.2_r1/android/widget/FrameLayout.java#678
Loading margins: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.0.2_r1/android/view/ViewGroup.java#6619
Layouting: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.0.2_r1/android/widget/LinearLayout.java#1539
View class does not contain margins. It contains the padding because padding offset the contents of the view. Margin is meant to offset the view itself from the parent, ViewGroup. As such, the margins are contained in the ViewGroup class.
However remember the principle of inheritance. A view is a child of the ViewGroup class. As a result, it inherits the properties of the ViewGroup, including the margins. So when you apply the margin on the view, it responds because it already has the margins properties by virtue of inheritance.
First, I want to list tags in a whatever layout. The container's max width is fixed (match_parent), and the tags (TextViews) should no exceed the right border of the container. Meanwhile, the container is a direct child (LinearLayout in my case) of a ScrollView (I emphasize this as I find some solutions using customized onMeasure and onLayout methods extending ViewGroup, but cannot work in ScrollView).
How can I do this?
This could be solved with FlowLayout things, as commented by WaJeEh.
I am programming my own layout, extending the ViewGroup class. One of the meethod you need to override is the onMeasure() method. In this method, the parent View (the view that will contain my layout) will pass the posible width and height my layout can have.
#Override
public class MyOwnLayout extends ViewGroup {
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Argumens passed by the parent View.
// Can be both arguments be as MeasureSpec.UNSPECIFIED simultaneously ??
}
}
This MeasureSpec arguments can have three possible modes: "EXACLY", "AT_MOST" and "UNSPECIFIED" (The meaning of them are here. Specifically, "UNSPECIFIED" value is passed by the parent when my layout can be of any size it wants in that specific dimension. This is given, for example, when the parent is a ScrollView.
So my only doubt is: It is possible for the parent to pass to my layout both of them as UNSPECIFIED simultaneously. If possible, in which cases?
You could make a custom view which scrolls in both directions. This would (or could) measure its children with UNSPECIFIED in both height and width. It's possible that a HorizontalScrollView nested in a ScrollView will cause this behaviour as well, but that isn't recommended.
Typically, this case (both UNSPECIFIED) will not come up. It's probably okay to not ignore it. However, I've found that ignoring technically legal configurations to be a good way to introduce weird bugs in the future. You should handle this case in your onMeasure if you can.
I have a LayoutView in a ScrollView named MyLayout and I want to add a View composed by LayoutView with TextView and EdiText inside this ScrollView according a value,
for example with a method
addMyCustomViewToMyLayout(int x){
....
}
that adds the desired number of elements to the MyLayout in the ScrollView,
The CustomViews added to the ScrollView should be linked to the activity to get and set the value from-to the app.
Is possible?
How could I do?
There's nothing very special about ScrollViews. You can think of them as a boundless View. While normal Layouts like a LinearLayout are bound to an area, then ScrollView will allow its child to be whatever height it wants to be (or width in the case of a HorizontalScrollView).
ScrollViews contain one child. The child size is bound by the width of the ScrollView, but it is not bound by the height. Thus, the child will measure itself based on the idea that it has enough vertical space to show everything.
In this case, you create or inflate your MyLayout however you want. Then you add it to the ScrollView. If you want to add other Views to it, you add it to your MyLayout layout view. Just let the ScrollView do its thing.
What is android here?
what is Orientation here?
What is Vertical ?
I would be pleased to know if they are classes or packages or methods..?
I am confused?
Can some one explain hierarchy of it?
I am sure you have seen this inside the <LinearLayout>.
It means that whatever view you take inside the LinearLayout will be shown in screen by vertical (like Stake of views).
Every attributes started with android followed by : so here orientation is an attribute and vertical is the value to be assigned this attribute.
Update:
(Answer taken from here.)
For android:orientation="vertical", your views get stacked vertically like this:
View1
View2
View3
View4
etc...
And For android:orientation="horizontal", your views gets placed horizontally like this:
View1 View2 View3 View4 etc...
This is the XML tag for the Layout properties of any Layout Widget for Android UI.
android:orientation is the XML tag and "vertical" is value for the same. so when it will be loaded in UI framework, child of the layout will be arranged in vertical form.
These are Input parameters for XML tags . Although Java is an object oriented language ,but it does not means that you will consider every element of android as Classes . XML Layout structure is view forming technique which use native kit internally .so these #android:something are just identifier to tell the native kit what to do . nothing else .
This is the code written in android.widget.LinearLayout.java
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
if (mOrientation == VERTICAL) {
layoutVertical();
} else {
layoutHorizontal();
}
}
You can view the SOURCE CODE HERE, Based on the orientation and gravity attribute how the android sets Child Views into Parent.