简体   繁体   English

以编程方式添加视图会导致Android中大小发生变化

[英]Adding views programmatically cause change of size in Android

I'm trying to add views programmatically but when i do, the size of a imageview is not the same (i've used it in another view and set by xml). 我正在尝试以编程方式添加视图,但是当我这样做时,imageview的大小是不一样的(我已经在另一个视图中使用它并由xml设置)。

This is my function that i use to add programmatically views : 这是我用来以编程方式添加视图的函数:

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);
        }
    }
}

This is caused because you are passing null while inflating your views. 这是因为您在扩大视图时传递null。

Lets consider the following view to be inflated(From a listViews point of view): 让我们考虑以下视图(从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>

If we are adding the above xml view to a listview as such: 如果我们将上述xml视图添加到listview中,如下所示:

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

    return convertView;
}

Result: 结果: 在此处输入图片说明

But with the right approach: 但是采用正确的方法:

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

    return convertView;
}

Result: 结果: 在此处输入图片说明

More info can be found in this nice post by Dave Smith. 可以在Dave Smith的这篇不错的文章中找到更多信息。

You too have to replace the null with the parent viewGroup. 您也必须将null替换为父viewGroup。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM