1

我正在开发一个安卓应用程序。我需要在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);
4

3 回答 3

4

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

或以编程方式

txt_peron_name.setGravity(Gravity.RIGHT);

也试试这个

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

于 2013-08-29T05:55:08.540 回答
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。所以这是我所做的小改动。

输出

在此处输入图像描述

于 2013-08-29T06:57:44.113 回答
0

@Hitesh Kamani 试试这个

TextView ta  = (TextView) findViewById(R.layout.text_view);
       LayoutParams lp = new LayoutParams();
       lp.gravity= Gravity.RIGHT; 
 ta.setLayoutParams(lp);
于 2013-08-29T06:42:22.713 回答