这是我到目前为止所做的:
LinearLayout category_layout = new LinearLayout(getActivity());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams (LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(15, 5, 5, 10);
category_layout.setLayoutParams(params);
//Add Category Title
TextView category_title = new TextView(getActivity());
category_title.setText("MEN " + i);
category_title.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
category_title.setPadding(10,0,0,0); category_title.setTextColor(getResources().getColor(R.color.text_color));
category_title.setTypeface(null, Typeface.BOLD);
category_layout.addView(category_title);
TextView category_more = new TextView(getActivity());
category_more.setText("MORE");
LinearLayout.LayoutParams lp_ineer_ver = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp_ineer_ver.gravity = Gravity.RIGHT;
category_more.setLayoutParams(lp_ineer_ver);
category_more.setPadding(0,0,10,0);
category_more.setTextColor(getResources().getColor(R.color.text_color));
category_more.setTypeface(null, Typeface.BOLD);
category_more.setGravity(Gravity.RIGHT);
category_layout.addView(category_more);
现在TextView"更多"不要出现,但如果我试着评论该行
category_layout.addView(category_title);
然后它显示并完美对齐。发生了什么?不管怎样,请帮忙!!!
答案 0 :(得分:1)
试试这个:
您需要在第一个textview
中将宽度设置为换行内容。
category_title.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
示例代码
LinearLayout category_layout = new LinearLayout(getActivity());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(15, 5, 5, 10);
category_layout.setLayoutParams(params);
//Add Category Title
TextView category_title = new TextView(getActivity());
category_title.setText("MEN " + i);
category_title.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
category_title.setPadding(10,0,0,0);
category_title.setTextColor(getResources().getColor(R.color.text_color));
category_title.setTypeface(null, Typeface.BOLD);
category_layout.addView(category_title);
TextView category_more = new TextView(getActivity());
category_more.setText("MORE");
LinearLayout.LayoutParams lp_ineer_ver = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp_ineer_ver.gravity = Gravity.RIGHT;
category_more.setLayoutParams(lp_ineer_ver);
category_more.setPadding(0,0,10,0);
category_more.setTextColor(getResources().getColor(R.color.text_color));
category_more.setTypeface(null, Typeface.BOLD);
category_more.setGravity(Gravity.RIGHT);
category_layout.addView(category_more);
答案 1 :(得分:0)
指定weightsum
和weights
将空间划分为相等的一半
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
category_layout.setWeightSum(2);
params.setMargins(15, 5, 5, 10);
category_layout.setLayoutParams(params);
LinearLayout.LayoutParams titleParams = (LinearLayout.LayoutParams) category_layout.getLayoutParams();
titleParams.weight = 1;
category_title.setGravity(Gravity.START);
category_title.setLayoutParams(titleParams);
LinearLayout.LayoutParams moreParams = (LinearLayout.LayoutParams) category_layout.getLayoutParams();
moreParams.weight = 1;
category_more.setGravity(Gravity.END);
category_more.setLayoutParams(moreParams);