简体   繁体   中英

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).

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.

Lets consider the following view to be inflated(From a listViews point of view):

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

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.

You too have to replace the null with the parent viewGroup.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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