2

我正在开发一个 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>

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

这是屏幕截图:

在此处输入图像描述

如您所见,它不会一半一半地占用屏幕。我该如何解决?

4

3 回答 3

1

Try this below in your 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"

And change your Java code:

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

But maybe your image will be stretched by the width. But it will fill half of the width as you want.

UPDATE:

I dont know why you tried failed with above solution. So I write a small demo for referencing. It uses ScrollView as you want. But I strongly recommend you should use GridView instead for better perfomance!!!

@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>

Screenshot: enter image description here

于 2016-05-18T11:01:04.927 回答
0

试试这个使用 straggerdgridlayout 管理器

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

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

column 替换了您对 column 的要求

于 2016-05-18T10:37:21.353 回答
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);

它可能会起作用。

于 2016-05-17T13:14:02.253 回答