Android - 以编程方式创建具有两个并排左对齐文本视图的居中布局

时间:2014-01-16 19:50:34

标签: java android relativelayout textview

标题应该是非常自我解释的。我正在尝试创建一个看起来像桌子的居中视图,并且在该视图中将并排显示两个文本视图。我需要这样做,因为两组文本的字体颜色会有所不同,后者会有一个onClick事件。

我可以并排创建两个视图,但是左对齐。我可以在相对视图中创建它们,但无论我做什么,文本都会成为中心对齐的。这是我到目前为止所做的:

public void addTableLink(String s, String s1, int g, LinearLayout L, int fsize, int textColor, int backgroundColor, int lpad, int tpad, final String section, final String selection){
       TextView tv = new TextView(this);
       RelativeLayout rl = new RelativeLayout(c);
       rl.setGravity(Gravity.CENTER);
       tv.setText(s);
       tv.setTextSize(fsize);

       TextPaint textPaint = tv.getPaint();
       int boundedWidth = 360;

       StaticLayout layout = new StaticLayout(s, textPaint, boundedWidth , Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
       int height = layout.getHeight();

       tv.setGravity(g);
       tv.setBackgroundColor(backgroundColor);

       tv.setLayoutParams(new RelativeLayout.LayoutParams(400, height + 5));

       tv.setPadding(lpad, tpad, lpad, 0);
       tv.setTextColor(textColor);

       TextView tv1 = new TextView(this);
       tv.setText(s1);
       tv.setTextSize(fsize);

       rl.addView(tv);

       textPaint = tv1.getPaint();

       layout = new StaticLayout(s1, textPaint, boundedWidth , Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
       height = layout.getHeight();

       tv1.setGravity(g);
       tv1.setBackgroundColor(backgroundColor);

       tv1.setLayoutParams(new RelativeLayout.LayoutParams(400, height + 5));

       tv1.setPadding(lpad, tpad, lpad, 0);
       tv1.setTextColor(textColor);

       tv1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {                  
                setMainView(section, selection);
             }
            });


       rl.addView(tv1);
       L.addView(rl);

}

这就是:

enter image description here

第二行说“B. Doe”是用不同的方法制作的,只是一个单一的文本视图(不需要更改字体颜色,也不需要onClick事件)。第三行是用上面的代码制作的,我希望它看起来像第二行。

0 个答案:

没有答案
相关问题