FlowLayout
FlowLayout copied to clipboard
Set margin programatically not work
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 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 thanks