【问题标题】:Unable to set layout width, height and weight of inflated view in XML in Android无法在 Android 中的 XML 中设置膨胀视图的布局宽度、高度和重量
【发布时间】:2017-02-19 21:09:30
【问题描述】:

我正在开发一个 Android 应用程序。在我的应用程序中,我需要动态扩充视图列表。我添加了它们并开始工作。问题在于设置布局的宽度和高度。现在我将用一个简单的项目来演示我的问题。实际上,我的项目比这个简单的项目要复杂得多。

我正在为这个布局增加视图。

           <LinearLayout
                android:orientation="horizontal"
                android:id="@+id/cm_photos_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            </LinearLayout>

我正在遍历位图列表并按照以下方式动态添加视图

for(Bitmap bmp : bitmaps)
{
     View preview = layoutInflater.inflate(R.layout.item_cm_preview_image,null);
                ImageView previewImageView = (ImageView)preview.findViewById(R.id.item_cm_preview_image);
                previewImageView.setImageBitmap(bmp);
     container.addView(preview);
}

请注意,在上面的代码中,container是一个LinearLayout,动态添加到上面的父XML中。

container = new LinearLayout(this);
                    container.setOrientation(LinearLayout.HORIZONTAL);
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    container.setLayoutParams(params);

parentLinearLayout.addView(container);

这是我的 item_cm_preview_image.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_weight="1"
    android:layout_height="400dp"
    android:layout_width="0dp"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <ImageView
        android:scaleType="centerCrop"
        android:id="@+id/item_cm_preview_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

如您所见,我在 XML 中将布局高度设置为 400dp,宽度 0 和 layout_weight 设置为 1。因此,由于 layout_weight,所有图像高度必须相同且宽度必须相等。但结果并不如预期。您可以在下面看到屏幕截图。

正如您在屏幕截图中看到的,layout_weight 和 height 都不适用于所有膨胀视图。但是,如果我动态添加额外的 ViewGroup 并将视图膨胀到该布局,它就可以工作。下面是我的代码

//This happening in for loop
LinearLayout wrapper = new LinearLayout(this);
                wrapper.setLayoutParams(new LinearLayout.LayoutParams(0,500,1));

                View preview = layoutInflater.inflate(R.layout.item_cm_preview_image,null);
                ImageView previewImageView = (ImageView)preview.findViewById(R.id.item_cm_preview_image);
                previewImageView.setImageBitmap(bmp);

                wrapper.addView(preview);
                container.addView(wrapper);

这是结果:

当我使用额外的动态线性布局时,您可以看到 layout_weight 和 height 都有效。为什么在 XML 中设置布局重量和高度不起作用?为什么第二种方法有效?如何在 XML 布局文件中设置重量和高度?有可能吗?

【问题讨论】:

    标签: android android-layout android-view layout-inflater android-viewgroup


    【解决方案1】:

    如果你使用方法膨胀布局

    View preview = layoutInflater.inflate(R.layout.item_cm_preview_image,null);
    

    它会跳过它的宽度和高度参数...,但如果你会使用:

    View preview = layoutInflater.inflate(R.layout.item_cm_preview_image,parent,false);
    

    它应该可以正常工作,例如,如果您在作为父级的活动中膨胀视图,您可以提供 (ViewGroup) getView():

    View preview = layoutInflater.inflate(R.layout.item_cm_preview_image,(ViewGroup) getView(), false);
    

    【讨论】:

    • 哦,非常感谢。我让它工作。这正是我想知道的。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 2011-12-22
    • 1970-01-01
    • 2015-11-23
    • 1970-01-01
    • 2022-01-21
    相关资源
    最近更新 更多