2013-08-29 34 views
1

我正在開發一個android應用程序。我需要顯示textview在右側的tablerow,下面是我使用的代碼,這個代碼有什麼錯?如何在androidrow中顯示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); 

回答

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

+1

非常感謝@Chintan Rathod。它工作得很好。 –

4

在您的XML中爲文本視圖指定權重。

或程序

txt_peron_name.setGravity(Gravity.RIGHT);

嘗試,這也

txt_peron_name.setLayoutParams(this.rowParams); //你行

+0

這是行不通的。我動態顯示tableLayout並在其中添加行,並且我希望textview txt_person_name顯示在行的右側。我怎麼能夠? –

+2

@HiteshKamani,加入gru的建議之後,代碼在正確的重力下運行良好。你應該檢查你的代碼。我已經實施並正常工作。 –

+0

@ChintanRathod,我被困住了。你可以在這裏發佈你的代碼嗎? –

0

@Hitesh Kamani 佈局PARAMS試試這個

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