【问题标题】:What's the default layout of an ImageView?ImageView 的默认布局是什么?
【发布时间】:2023-03-19 16:50:01
【问题描述】:

在 xml 中创建时,需要参数 layout_width 和 layout_height。
用 Java 代码创建的项目呢?他们的默认布局是什么?
如何以编程方式将其布局设置为 fill_parent 或 wrap_content?

【问题讨论】:

    标签: android layout


    【解决方案1】:

    public void addView(查看子视图) 自:API 级别 1

    添加子视图。如果尚未在子级上设置布局参数,则在子级上设置此 ViewGroup 的默认参数。 参数 child 要添加的子视图 另请参阅

    generateDefaultLayoutParams()
    

    没有附加参数,addView(View) 使用默认参数(主要是WRAP_CONTENT)。看着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;
    }
    

    【讨论】:

      猜你喜欢
      • 2013-01-08
      • 2011-02-26
      • 2012-11-29
      • 1970-01-01
      • 2011-02-24
      • 2015-06-21
      • 1970-01-01
      相关资源
      最近更新 更多