Set "gravity" attribute to a RelativeLayout not working
2017-03-07 18:28
531 查看
How
the subviews is shown in the image below.
I resized the widths of all the subviews
so that what is happening is more clear.
Note that
the way
gravity is to take all the subviews as a group and move them around the layout. This means that whichever
view is widest will determine how everything else is positioned. So gravity in a Relative layout is probably only useful if all the subviews have the same width.
When you add
a
it does arrange the subviews as one would expect.
For example, one could "save code" by setting the
That way there is no need individually set the
each subview. See the various options in the image below.
Note that when a view uses
it overrides the LinearLayout's gravity. (This can be seen in the title for the two layouts in the left image. The LinearLayout
set to
but the title TextView's
set to
When positioning views within a RelativeLayout,
the general way to do it is adding things like the following to each view:
If one wants to set all the views at once, a
likely be better (or perhaps using a Style).
So to sum up,
The
not work for subviews in a RelativeLayout.
The
a RelativeLayout does work, but not as one might expect.
From: http://stackoverflow.com/questions/42142058/why-doesnt-gravity-work-when-applied-to-relative-layout/42153928#42153928
gravityaffects
the subviews is shown in the image below.
I resized the widths of all the subviews
so that what is happening is more clear.
Note that
the way
RelativeLayouthandles
gravity is to take all the subviews as a group and move them around the layout. This means that whichever
view is widest will determine how everything else is positioned. So gravity in a Relative layout is probably only useful if all the subviews have the same width.
Linear Layout with gravity
When you add gravityto
a
LinearLayout,
it does arrange the subviews as one would expect.
For example, one could "save code" by setting the
gravityof
LinearLayoutto
center_horizontally.
That way there is no need individually set the
layout_gravityof
each subview. See the various options in the image below.
Note that when a view uses
layout_gravity,
it overrides the LinearLayout's gravity. (This can be seen in the title for the two layouts in the left image. The LinearLayout
gravitywas
set to
leftand
right,
but the title TextView's
layout_gravitywas
set to
center_horizontally.)
Final notes
When positioning views within a RelativeLayout,the general way to do it is adding things like the following to each view:
layout_alignParentTop
layout_centerVertical
layout_below
layout_toRightOf
If one wants to set all the views at once, a
LinearLayoutwould
likely be better (or perhaps using a Style).
So to sum up,
The
layout_gravitydoes
not work for subviews in a RelativeLayout.
The
gravityof
a RelativeLayout does work, but not as one might expect.
From: http://stackoverflow.com/questions/42142058/why-doesnt-gravity-work-when-applied-to-relative-layout/42153928#42153928
相关文章推荐
- When the layout is set to FIXED LAYOUT, the VERTICAL ALIGNMENT is not working. As soon as changed to
- QWidget::setLayout: Attempting to set QLayout "" on MainWindow "", which already has a layout
- setLayout: Attempting to set QLayout "" on MainWindow "MainWindow", which already has a layout
- PercentRelativeLayout, how to set the height programatically
- set OpenCV_FOUND to FALSE so package "OpenCV" is considered to be NOT FOUND.
- Visual Studio 2015打开ASP.NET MVC的View提示"Object reference not set to an instance of an object"错误的解决方案
- <c:set var="root" value="${pageContext.request.contextPath}"></c:set>According to TLD or attribute d
- how to solve "[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!"
- rsync: failed to set times on "/." (in ftp): Operation not permitted (1)
- 使用RelativeLayout时,android:layout_toEndOf="@/id/xxxx",还是"@+id/xxxx"
- Can't process attribute android:fillColor="@color/text_gray": references to other resources are not
- 添加wcf服务时弹出"an object reference not set to an instance of an object "
- NET:Error Creating Control -"Object Reference Not Set To An Instance Of Object"
- setLayout: Attempting to set QLayout "" on MainWindow "MainWindow", which already has a layout
- object reference not set to an instance of an object" - Not "initialized" through WCF?
- Qt提示:setLayout: Attempting to set QLayout "" on MainWindow "MainWindow", which already has a layout
- QWidget::setLayout: Attempting to set QLayout "" on MainWindow "", which already has a layout
- QWidget::setLayout: Attempting to set QLayout "" on MainWindow "", which already has a layout
- RelativeLayout 在SDK 16(Android4.0)上 android:gravity="left" 属性失效解决
- Android RelativeLayout marginRight not working