Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set margin programatically not work #25

Open
zdd opened this issue Feb 1, 2016 · 2 comments
Open

Set margin programatically not work #25

zdd opened this issue Feb 1, 2016 · 2 comments
Assignees

Comments

@zdd
Copy link

zdd commented Feb 1, 2016

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);
        }
    }
@yisizhu520
Copy link

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

@zdd
Copy link
Author

zdd commented Jun 13, 2016

@yisizhu520 thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants