[英]how to display textview to right side in tablerow in android
我正在開發一個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);
將重力權限分配給XML中的文本視圖。
或以編程方式
txt_peron_name.setGravity(Gravity.RIGHT);
試試這個
txt_peron_name.setLayoutParams(this.rowParams);
//行的布局參數
@Hitesh Kamani試試這個
TextView ta = (TextView) findViewById(R.layout.text_view);
LayoutParams lp = new LayoutParams();
lp.gravity= Gravity.RIGHT;
ta.setLayoutParams(lp);
我實現了這段代碼。
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。 所以這是我所做的微小改動。
產量
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.