FlowLayout icon indicating copy to clipboard operation
FlowLayout copied to clipboard

Set margin programatically not work

Open zdd opened this issue 9 years ago • 2 comments

If I set the margin by code instead of xml, there is no margin show up. here is the code. searchHistory is the type of FlowLayout

private void loadSearchHistoryFromDB() {
        ArrayList<String> keyWords = myDatabase.getAllBeitieSearchHistory();
        for (final String keyword: keyWords) {
            TextView textView = new TextView(getActivity());
            textView.setText(keyword);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
            textView.setBackgroundResource(R.drawable.tag_text);
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    searchBeitieInDB(keyword);
                }
            });

            ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            lp.leftMargin = 15;
            lp.rightMargin = 15;
            lp.topMargin = 15;
            lp.bottomMargin = 15;

            searchHistory.addView(textView, lp);
        }
    }

zdd avatar Feb 01 '16 09:02 zdd

@zdd the margin value will be ignored when you add child view with ViewGroup.MarginLayoutParams instead of FlowLayout.LayoutParams.

The following code can solve the problem:

FlowLayout.LayoutParams lp = new FlowLayout.LayoutParams(FlowLayout.LayoutParams.WRAP_CONTENT, FlowLayout.LayoutParams.WRAP_CONTENT);

yisizhu520 avatar May 26 '16 06:05 yisizhu520

@yisizhu520 thanks

zdd avatar Jun 13 '16 13:06 zdd