以编程方式添加视图会导致Android中的大小更改

时间:2014-05-19 12:30:57

标签: java android view

我尝试以编程方式添加视图,但是当我这样做时,imageview的大小不一样(我已在另一个视图中使用它并由xml设置)。

这是我用来以编程方式添加视图的函数:

public void initClaps(int size, List<Clap> claps) {
    LayoutParams params = new LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT);
    //LayoutParams params2 = new LayoutParams(0, LayoutParams.WRAP_CONTENT,
    //      33);
    LayoutParams params3 = new LayoutParams(0,
            LayoutParams.WRAP_CONTENT, 1.0f);
    int position = 0;
    for (int i = 0; i < size; i++) {
        LinearLayout layout = new LinearLayout(getActivity());
        layout.setWeightSum(3.0f);
        layout.setOrientation(LinearLayout.HORIZONTAL);
        if (i == 0) {
            View view_add = in.inflate(R.layout.adapter_claps2, null);
            layout.addView(view_add, params3);

            for (int j = 0; j < 2; j++) {
                View view_clap = in.inflate(R.layout.adapter_claps, null);
                final Clap clap = claps.get(position);
                FrameLayout fl = (FrameLayout) view_clap
                        .findViewById(R.id.fl_adapter_claps);
                SmartImageViewRound siv = (SmartImageViewRound) view_clap
                        .findViewById(R.id.siv_adapter_claps);
                TextView tv = (TextView) view_clap
                        .findViewById(R.id.tv_adapter_claps);
                siv.setImageUrl(clap.getMini());
                tv.setText("" + clap.getNom());
                fl.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(getActivity(),
                                ClapDetail.class);
                        intent.putExtra(ClapDetail.INTENT_NOM,
                                clap.getNom());
                        getActivity().startActivity(intent);
                    }
                });
                layout.addView(view_clap, params3);
                position++;
            }
            ll_container.addView(layout, params);
        } else {
            for (int j = 0; j < 3; j++) {
                if(position != claps.size()){
                    final Clap clap = claps.get(position);
                    View view = in.inflate(R.layout.adapter_claps, null);
                    FrameLayout fl = (FrameLayout) view
                            .findViewById(R.id.fl_adapter_claps);
                    SmartImageViewRound siv = (SmartImageViewRound) view
                            .findViewById(R.id.siv_adapter_claps);
                    TextView tv = (TextView) view
                            .findViewById(R.id.tv_adapter_claps);
                    siv.setImageUrl(clap.getMini());
                    tv.setText("" + clap.getNom());
                    fl.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(getActivity(),
                                    ClapDetail.class);
                            intent.putExtra(ClapDetail.INTENT_NOM,
                                    clap.getNom());
                            getActivity().startActivity(intent);
                        }
                    });
                    layout.addView(view, params3);
                    position++;
                }

            }
            ll_container.addView(layout, params);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

这是因为您在扩充视图时传递null。

让我们考虑以下视图进行夸大(从listViews的角度来看):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:orientation="horizontal">
    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingRight="15dp"
        android:text="Text1" />
    <TextView
        android:id="@+id/text2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Text2" />
</LinearLayout>

如果我们将上述xml视图添加到listview中:

public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = inflate(R.layout.item_row, null);
    }

    return convertView;
}

结果: enter image description here

但采用正确的方法:

public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = inflate(R.layout.item_row, parent, false);
    }

    return convertView;
}

结果: enter image description here

Dave Smith在this nice post中可以找到更多信息。

您还必须将null替换为父视图组。