简体   繁体   English

ImageView 的默认布局是什么?

[英]What's the default layout of an ImageView?

When creating it in xml, the params layout_width and layout_height are required.在 xml 中创建时,需要参数 layout_width 和 layout_height。
What about items created in Java code?在 Java 代码中创建的项目呢? What's their default layout?他们的默认布局是什么?
How can I set their layout to fill_parent or wrap_content programmatically?如何以编程方式将其布局设置为 fill_parent 或 wrap_content?

public void addView (View child) Since: API Level 1 public void addView (View child) 自:API Level 1

Adds a child view.添加子视图。 If no layout parameters are already set on the child, the default parameters for this ViewGroup are set on the child.如果尚未在子级上设置布局参数,则在子级上设置此 ViewGroup 的默认参数。 Parameters child the child view to add See Also参数 child 要添加的子视图 另请参阅

generateDefaultLayoutParams()

Without additional parameters, addView(View) uses the default parameters (mostly WRAP_CONTENT ).没有附加参数, addView(View)使用默认参数(主要是WRAP_CONTENT )。 Looking at the source code ,源代码

protected LayoutParams  [More ...] generateDefaultLayoutParams() {
    if (mOrientation == HORIZONTAL) {
        return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    } else if (mOrientation == VERTICAL) {
        return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    }
    return null;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM