将布局权重设置为动态添加的视图在Android中不起作用

时间:2016-05-17 13:09:56

标签: android android-view android-viewgroup

我正在开发Android应用程序。在我的应用程序中,我正在动态添加视图并在代码中设置其权重。但它没有用。请参阅下面的代码。

这是我的动态视图容器的xml

<ScrollView>
.
.
.

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

.
.
.
</ScrollView>

这就是我动态添加控件的方式。

private void addRelatedItemViews(final ArrayList<Item> relatedItems)
    {
        LinearLayout subContainer= new LinearLayout(this);
        LinearLayout.LayoutParams initialParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        subContainer.setLayoutParams(initialParam);
        relatedItemsContainer.addView(subContainer);
        for(int i=0;i<relatedItems.size();i++)
        {
            if(i>0 && i%2==0)
            {
                //initialize sub container
                subContainer = new LinearLayout(this);
                LinearLayout.LayoutParams subContainerParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                subContainer.setLayoutParams(subContainerParam);
                relatedItemsContainer.addView(subContainer);
            }
            final int index = i;
            View view = layoutInflater.inflate(R.layout.realated_item_view,null);
            ImageView imageView = (ImageView)view.findViewById(R.id.iv_related_item_image);
            TextView tvName = (TextView)view.findViewById(R.id.tv_related_item_name);
            TextView tvPrice = (TextView)view.findViewById(R.id.tv_related_item_price);
            TextView tvLikeCount = (TextView)view.findViewById(R.id.tv_related_item_like_count);
            Picasso.with(getBaseContext()).load(relatedItems.get(i).getMediumImageUrl()).into(imageView);
            tvName.setText(relatedItems.get(i).getName());
            tvPrice.setText(CommonHelper.formatCurrency(relatedItems.get(i).getPrice()));
            tvLikeCount.setText(CommonHelper.formatLikeCount(relatedItems.get(i).getLikeCount()) + " like");
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    openItemActivity(relatedItems.get(index).getId());
                }
            });
            LinearLayout itemContainer = new LinearLayout(this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT,1);
            itemContainer.setLayoutParams(params);
            itemContainer.addView(view);
            subContainer.addView(itemContainer);
        }
    }

这是我的realated_item_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="wrap_content"
    android:layout_weight="1"
    android:padding="3dp"

    android:layout_height="wrap_content">
    <LinearLayout
        android:background="@drawable/item_bg"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/iv_related_item_image"
            android:scaleType="centerCrop"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <LinearLayout
            android:padding="5dp"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:textStyle="bold"
                android:textSize="17dp"
                android:id="@+id/tv_related_item_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <TextView
                android:layout_marginTop="3dp"
                android:textSize="13dp"
                android:id="@+id/tv_related_item_price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <TextView
                android:layout_marginTop="3dp"
                android:textSize="10dp"
                android:id="@+id/tv_related_item_like_count"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

但它没有正确设置它的重量。实际上,每行有两列,每列必须是屏幕宽度的一半。但它没有用。

这是截图:

enter image description here

正如你所看到的,它没有将屏幕减半。我该如何解决?

3 个答案:

答案 0 :(得分:0)

而不是ViewGroup.LayoutParams尝试使用LinearLayout.LayoutParams并将布局宽度设置为0

LinearLayout itemContainer = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT,1);
itemContainer.setLayoutParams(params);

它可能有用。

答案 1 :(得分:0)

试试这个 使用straggerdgridlayout manager

RecyclerView recyclerView =(RecyclerView)findViewById(R.id.recycler_view);         recyclerView.setHasFixedSize(真);

    gaggeredGridLayoutManager = new StaggeredGridLayoutManager(column, 1);
    recyclerView.setLayoutManager(gaggeredGridLayoutManager);

列取代了您对列

的要求

答案 2 :(得分:0)

在realated_item_view.xml中尝试以下内容(wrap_content to fill_parent)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical" android:layout_width="fill_parent"
   android:layout_weight="1"
   android:padding="3dp"

并更改您的Java代码:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,1);
        itemContainer.setLayoutParams(params);
        itemContainer.addView(view);
        subContainer.addView(itemContainer);

但也许你的图像会被宽度拉伸。但它会填满你想要的宽度的一半。

更新:

我不知道为什么你尝试使用上述解决方案失败了。所以我写了一个小的演示用于引用。它可以根据需要使用ScrollView。但我强烈建议您使用GridView代替更好的性能!

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test_layout);

    LinearLayout containerLayout = (LinearLayout) findViewById(R.id.container_layout);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

    LayoutInflater inflater = getLayoutInflater();
    for (int i = 0; i < 4; i ++){
        LinearLayout subLayout = new LinearLayout(this);
        subLayout.setOrientation(LinearLayout.HORIZONTAL);
        LinearLayout.LayoutParams subParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
        for (int j = 0; j < 3; j ++){
            View v = inflater.inflate(R.layout.test_item_layout, null, false);
            if (j == 1){
                ImageView imageView = (ImageView) v.findViewById(R.id.imageView);
                imageView.setImageResource(R.drawable.icon);
            }
            subLayout.addView(v, subParams);
        }
        containerLayout.addView(subLayout, params);
    }

}

test_item_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
    android:id="@+id/imageView"
    android:src="@mipmap/ic_launcher"
    android:scaleType="centerCrop"
    android:layout_width="match_parent"
    android:layout_height="100dip" />
<TextView
    android:text="Test label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</LinearLayout>

test_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/container_layout"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </LinearLayout>
</ScrollView>

</LinearLayout>

屏幕截图:enter image description here