在Android的设置布局参数 [英] Setting Layout parameters in android

查看:130
本文介绍了在Android的设置布局参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与XML文件的工作很容易,因为我可以指定的参数

Working with the XML file was easy as I could specify the parameters as

<android:layout_width="fill_parent" android:layout_height="wrap_content">

不过,我很困惑,同时通过code指定它。对于每一个观点我使用指定的参数

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)); 

我看到,我将其指定为相对布局,框架布局等的选择 截至目前我使用所有的意见,如图像,文本,也GridView的线性布局。如果视图参数基于父元素的布局来定义?还是确定将其指定为线性布局,即使观点是,比如说一个孩子,一个的FrameLayout?很抱歉,但我无法找出差异。

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.

推荐答案

所有的布局类(的LinearLayout RelativeLayout的等),延长的ViewGroup

All layout classes (LinearLayout, RelativeLayout, etc.) extend ViewGroup.

的ViewGroup 类有两个静态内部类:的LayoutParams MarginLayoutParams 。而 ViewGroup.MarginLayoutParams 实际上是扩展了 ViewGroup.LayoutParams

The ViewGroup class has two static inner classes: LayoutParams and MarginLayoutParams. And ViewGroup.MarginLayoutParams actually extends ViewGroup.LayoutParams.

有时候布局类需要与子视图关联的其他布局信息。为此,他们明确自己的内部静态的LayoutParams 类。例如,的LinearLayout 有:

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:

public class LinearLayout extends ViewGroup {
   ...
   public static class LayoutParams extends ViewGroup.MarginLayoutParams {  
   ...
   }
}

同样的事情 RelativeLayout的

public class RelativeLayout extends ViewGroup {
   ...
   public static class LayoutParams extends ViewGroup.MarginLayoutParams {  
   ...
   }
}

LinearLayout.LayoutParams RelativeLayout.LayoutParams 是完全不同的独立的类。它们存储关于孩子的观点不同的附加信息。

But LinearLayout.LayoutParams and RelativeLayout.LayoutParams are completely different independent classes. They store different additional information about child views.

例如, LinearLayout.LayoutParams 可以重量价值与每个视图相关联,而 RelativeLayout.LayoutParams 不能。 值相关联:同样的事情 RelativeLayout.LayoutParams C $ C>, alightWithParent 每个视图。而 LinearLayout.LayoutParams 根本不具备这些能力。

For example, LinearLayout.LayoutParams can associate weight value with each view, while RelativeLayout.LayoutParams can't. Same thing with RelativeLayout.LayoutParams: it can associate values like above, below, alightWithParent with each view. And LinearLayout.LayoutParams simply don't have these capability.

因此​​,在一般情况下,你必须使用的LayoutParams 从封闭布局,让您的观点正确定位和呈现。但是请注意,所有的LayoutParams 具有相同的父类 ViewGroup.LayoutParams 。如果你只使用在该类中定义的功能(如在你的情况 WRAP_CONTENT FILL_PARENT ),你可以得到工作code,即使错了的LayoutParams 类是用于指定布局PARAMS。

So in general, you have to use LayoutParams from enclosing layout to make your view correctly positioned and rendered. But note that all LayoutParams have same parent class ViewGroup.LayoutParams. And if you only use functionality that is defined in that class (like in your case WRAP_CONTENT and FILL_PARENT) you can get working code, even though wrong LayoutParams class was used to specify layout params.

这篇关于在Android的设置布局参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆