简体   繁体   English

向LinearLayout中添加2个TextView(一个向左对齐,一个向右对齐)

[英]Add 2 TextView into LinearLayout(one align left and one align right)

Here is what I've done so far: 到目前为止,这是我所做的:

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

Now the TextView "More" don't show up,but If I try to comment the line 现在,TextView“更多”不会显示,但是如果我尝试注释该行

category_layout.addView(category_title);

then it show and align right perfectly. 然后显示并正确对齐。 What happened? 发生了什么? Any idea, Please help!!! 任何想法,请帮助!!!

Try this: 尝试这个:

You need to set width to Wrap content in first textview . 您需要设置宽度以在第一个textview包装内容。

category_title.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CON‌​TENT,
LinearLayout.LayoutParams.WRAP_CONTENT));

sample code 样例代码

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_CON‌​TENT, 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);

Assign weightsum and weights to divide space into equal halfs 分配weightsumweights来划分空间分成相等的半区

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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