在android中设置布局参数

发布于 2024-11-16 23:18:31 字数 445 浏览 4 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷血 2024-11-23 23:18:31

所有布局类(LinearLayoutRelativeLayout 等)都扩展了 ViewGroup

ViewGroup 类有两个静态内部类:LayoutParamsMarginLayoutParamsViewGroup.MarginLayoutParams 实际上扩展了 ViewGroup.LayoutParams


有时布局类需要额外的布局信息才能与子视图关联。为此,他们定义了内部静态 LayoutParams 类。例如,LinearLayout 具有:

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.LayoutParamsRelativeLayout.LayoutParams 完全不同独立班级。它们存储有关子视图的不同附加信息。

例如,LinearLayout.LayoutParams 可以将 weight 值与每个视图关联,而 RelativeLayout.LayoutParams 则不能。与 RelativeLayout.LayoutParams 相同:它可以将 abovebelowalightWithParent 等值与每个视图关联起来。而 LinearLayout.LayoutParams 根本不具备这些功能。


因此,一般来说,您必须使用封闭布局中的 LayoutParams 来使视图正确定位和渲染。但请注意,所有 LayoutParams 都有相同的父类 ViewGroup.LayoutParams。如果您只使用该类中定义的功能(例如您的情况 WRAP_CONTENTFILL_PARENT),即使 LayoutParams 错误,您也可以获得工作代码code> class 用于指定布局参数。

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

The ViewGroup class has two static inner classes: LayoutParams and MarginLayoutParams. And ViewGroup.MarginLayoutParams actually extends ViewGroup.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:

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

Same thing for RelativeLayout:

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

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

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.


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.

鲜血染红嫁衣 2024-11-23 23:18:31

根据您想要更改布局的视图数量,我认为最好创建一个辅助方法,并将您想要更改的任何视图以及您希望它们更改为的高度和宽度值传递给该方法:

public void setWidthHeight(View v, int width, int height){
    LayoutParams lp;
    lp = v.getLayoutParams();
    lp.width = width;
    lp.height = height;
    v.setLayoutParams(lp);
}

记住该设置这里的宽度和高度不会与 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:

public void setWidthHeight(View v, int width, int height){
    LayoutParams lp;
    lp = v.getLayoutParams();
    lp.width = width;
    lp.height = height;
    v.setLayoutParams(lp);
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文