如何在android中的tablerow中向右侧显示textview

时间:2013-08-29 05:52:10

标签: android textview tablerow

我正在开发一个Android应用程序。我需要在tablerow的右侧显示textview,下面是我正在使用的代码,这段代码有什么问题???

ScrollView src;
src=(ScrollView)findViewById(R.id.scrollView1);

TableLayout t1=new TableLayout(context);

TableRow.LayoutParams tableRowParams=new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.FILL_PARENT);                        
tableRowParams.gravity=Gravity.RIGHT | Gravity.CENTER_VERTICAL;

for(int i=0;i<count;i++)
{
    TableRow tr=new TableRow(context);
    TextView txt_peron_name=new TextView(context);
    txt_peron_name.setTextColor(Color.BLACK);
    txt_peron_name.setTextSize(15);
    txt_peron_name.setText("Hello");
    //txt_peron_name.setLayoutParams(tableRowParams);
    tr.setBackgroundResource(R.drawable.cc1);
    //tr.setLayoutParams(tableRowParams);
    tr.addView(txt_peron_name);
    t1.addView(tr,tableRowParams);

}// End of For Loop
src.removeAllViews();   

src.addView(t1);

3 个答案:

答案 0 :(得分:4)

将重力权限分配给XML中的文本视图。

或以编程方式

txt_peron_name.setGravity(Gravity.RIGHT);

试试这个

txt_peron_name.setLayoutParams(this.rowParams); //行的布局参数

答案 1 :(得分:0)

@Hitesh Kamani 试试这个

TextView ta  = (TextView) findViewById(R.layout.text_view);
       LayoutParams lp = new LayoutParams();
       lp.gravity= Gravity.RIGHT; 
 ta.setLayoutParams(lp);

答案 2 :(得分:0)

我实现了这段代码。

ScrollView sv = new ScrollView(StackDemosActivity.this);
TableLayout t1 = new TableLayout(StackDemosActivity.this);

TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams(
        TableRow.LayoutParams.MATCH_PARENT,
        TableRow.LayoutParams.MATCH_PARENT);
tableRowParams.gravity = Gravity.RIGHT | Gravity.CENTER_VERTICAL;

for (int i = 0; i < 25; i++) {
    TableRow tr = new TableRow(StackDemosActivity.this);
    TextView txt_peron_name = new TextView(StackDemosActivity.this);
    txt_peron_name.setTextColor(Color.BLACK);
    txt_peron_name.setTextSize(15);
    txt_peron_name.setText("Hello");
    tr.addView(txt_peron_name);
    tr.setGravity(Gravity.RIGHT);
    t1.addView(tr, tableRowParams);
}// End of For Loop
ll = (LinearLayout) findViewById(R.id.ll);
ll.removeAllViews();
sv.addView(t1);
ll.addView(sv);

我改变了一些代码,比如使用LinearLayout然后在里面添加ScrollView。所以这是我所做的微小改动。

<强>输出

enter image description here