目前,我手动设置瓷砖的宽度等于屏幕宽度的一半.这样做效果很好,但是它会在拼贴之间添加分隔线(如屏幕截图所示).幸运的是,似乎在API 21,there is now support for weight in GridLayout中引用了这里以方便您:
As of API 21,GrIDLayout’s distribution of excess space accomodates
the principle of weight. In the event that no weights are specifIEd,
the prevIoUs conventions are respected and columns and rows are taken
as flexible if their vIEws specify some form of alignment within their
groups. The flexibility of a vIEw is therefore influenced by its
alignment which is,in turn,typically defined by setting the gravity
property of the child’s layout parameters. If either a weight or
alignment were defined along a given axis then the component is taken
as flexible in that direction. If no weight or alignment was set,the
component is instead assumed to be inflexible.Multiple components in the same row or column group are consIDered to
act in parallel. Such a group is flexible only if all of the
components within it are flexible. Row and column groups that sit
either sIDe of a common boundary are instead consIDered to act in
serIEs. The composite group made of these two elements is flexible if
one of its elements is flexible.To make a column stretch,make sure all of the components insIDe it
define a weight or a gravity. To prevent a column from stretching,
ensure that one of the components in the column does not define a
weight or a gravity.When the principle of flexibility does not provIDe complete
disambiguation,GrIDLayout’s algorithms favour rows and columns that
are closer to its right and bottom edges. To be more precise,
GrIDLayout treats each of its layout parameters as a constraint in the
a set of variables that define the grID-lines along a given axis.
During layout,GrIDLayout solves the constraints so as to return the
unique solution to those constraints for which all variables are
less-than-or-equal-to the corresponding value in any other valID
solution.
我将这个由GrIDLayout和2列组成的简单布局放在一起,尝试实现两个同等宽度的列:
<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:paddingleft="@dimen/activity_horizontal_margin" androID:paddingRight="@dimen/activity_horizontal_margin" androID:paddingtop="@dimen/activity_vertical_margin" androID:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <GrIDLayout androID:layout_wIDth="match_parent" androID:layout_height="fill_parent" androID:layout_alignParenttop="true" androID:layout_centerHorizontal="true" androID:columnCount="2"> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_gravity="fill_horizontal" androID:text="Hello" androID:layout_columnWeight="1" /> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_gravity="fill_horizontal" androID:text="Hello" androID:layout_columnWeight="1" /> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_gravity="fill_horizontal" androID:text="Hello" androID:layout_columnWeight="1" /> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_gravity="fill_horizontal" androID:text="Hello" androID:layout_columnWeight="1" /> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_gravity="fill_horizontal" androID:text="Hello" androID:layout_columnWeight="1" /> </GrIDLayout></relativeLayout>
我认为,因为所有的孩子都有相同的柱重,所以会产生同样宽的柱子.然而,情况似乎并非如此,因为这是结果:
第一列比第二列窄.这里有什么我想念的吗?我是否误解了GrIDLayout的重量如何?
作为另一个问题,我似乎不能用重量技巧做0宽度以确保相同的宽度而不管内容(TextVIEw只是消失).如何使用GrIDLayout完成此 *** 作?
解决方法 只需将androID:layout_wIDth =“0dp”添加到GrIDLayout的每个子节点(任何没有layout_wIDth =“0dp”的子节点都会破坏权重布局.)如果您具有指定宽度大小的子视图,请确保没有子视图的宽度大于平均宽度大小.
希望它会对你有所帮助.
总结以上是内存溢出为你收集整理的Android GridLayout API 21全部内容,希望文章能够帮你解决Android GridLayout API 21所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)