在android中设置布局参数
使用 XML 文件很容易,因为我可以将参数指定为
<android:layout_width="fill_parent" android:layout_height="wrap_content">
但在通过代码指定它时我很困惑。对于每个视图,我使用 I see I 指定参数
view.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
,我可以选择将其指定为相对布局、框架布局等。 截至目前,我对所有视图(例如图像、文本和网格视图)使用线性布局。视图参数是否应该根据父元素的布局来定义?或者即使视图是框架布局的子级,也可以将其指定为线性布局吗?抱歉,但我无法找出其中的区别。
Working with the XML file was easy as I could specify the parameters as
<android:layout_width="fill_parent" android:layout_height="wrap_content">
But I am confused while specifying it through code. For each view I specify the parameters using
view.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
I see that I have an option of specifying it as relative layout, frame layout etc.
As of now I am using linear layout for all views such as images, text and also gridview. Should the view parameters be defined based on the layout of the parent element? Or is it OK to specify it as linear layout even if the view is a child of, say, a framelayout? Sorry, but I couldn't find out the difference.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所有布局类(
LinearLayout
、RelativeLayout
等)都扩展了ViewGroup
。ViewGroup
类有两个静态内部类:LayoutParams
和MarginLayoutParams
。ViewGroup.MarginLayoutParams
实际上扩展了ViewGroup.LayoutParams
。有时布局类需要额外的布局信息才能与子视图关联。为此,他们定义了内部静态 LayoutParams 类。例如,
LinearLayout
具有:RelativeLayout
具有相同的功能:但是
LinearLayout.LayoutParams
和RelativeLayout.LayoutParams
完全不同独立班级。它们存储有关子视图的不同附加信息。例如,
LinearLayout.LayoutParams
可以将weight
值与每个视图关联,而RelativeLayout.LayoutParams
则不能。与RelativeLayout.LayoutParams
相同:它可以将above
、below
、alightWithParent
等值与每个视图关联起来。而 LinearLayout.LayoutParams 根本不具备这些功能。因此,一般来说,您必须使用封闭布局中的 LayoutParams 来使视图正确定位和渲染。但请注意,所有
LayoutParams
都有相同的父类ViewGroup.LayoutParams
。如果您只使用该类中定义的功能(例如您的情况WRAP_CONTENT
和FILL_PARENT
),即使LayoutParams
错误,您也可以获得工作代码code> class 用于指定布局参数。All layout classes (
LinearLayout
,RelativeLayout
, etc.) extendViewGroup
.The
ViewGroup
class has two static inner classes:LayoutParams
andMarginLayoutParams
. AndViewGroup.MarginLayoutParams
actually extendsViewGroup.LayoutParams
.Sometimes layout classes need extra layout information to be associated with child view. For this they define their internal static
LayoutParams
class. For example,LinearLayout
has:Same thing for
RelativeLayout
:But
LinearLayout.LayoutParams
andRelativeLayout.LayoutParams
are completely different independent classes. They store different additional information about child views.For example,
LinearLayout.LayoutParams
can associateweight
value with each view, whileRelativeLayout.LayoutParams
can't. Same thing withRelativeLayout.LayoutParams
: it can associate values likeabove
,below
,alightWithParent
with each view. AndLinearLayout.LayoutParams
simply don't have these capability.So in general, you have to use
LayoutParams
from enclosing layout to make your view correctly positioned and rendered. But note that allLayoutParams
have same parent classViewGroup.LayoutParams
. And if you only use functionality that is defined in that class (like in your caseWRAP_CONTENT
andFILL_PARENT
) you can get working code, even though wrongLayoutParams
class was used to specify layout params.根据您想要更改布局的视图数量,我认为最好创建一个辅助方法,并将您想要更改的任何视图以及您希望它们更改为的高度和宽度值传递给该方法:
记住该设置这里的宽度和高度不会与 xml 中的相同值匹配,即 android:layout_width="32dp" 与 lp.width = 32 不同;
另外,名为 lp 的 LayoutParams 类型变量应该是视图返回的类型...检查视图返回的类型并在导入语句中导入该类型。
Depending on how many views you want to change the layouts on, I think it's better to create a helper method and pass whatever views you want to change to the method along with the height and width values you want them to change to:
Remember that setting the width and height here are not going to match the same values in your xml, i.e., android:layout_width="32dp" is not the same as lp.width = 32;
Also, the LayoutParams type variable called lp should be of the type returned by your view... Check what type is returned by the view and import that type in your import statements.