2011-06-08 196 views
1

我想對齊左邊的TextView和右邊的TextView,每一行都包含一個文本視圖... 我不想使用Width:fill_parentsetGravity(Gravity.Right)因爲我使用的是背景。 這裏是我的代碼:使用LayoutParam以編程方式安排TextView佈局

LinearLayout mainChatLayout = new LinearLayout(main.this); 
//ChatText.setId((int)System.currentTimeMillis()); 
lp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 
((android.widget.RelativeLayout.LayoutParams) lp).addRule(RelativeLayout.BELOW, recent.getId()); 
tv = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
//ChatText.setBackgroundColor(0xfff00000);#800080 
if(recent.getId() % 2 == 0) { 
    ChatText = new TextView(main.this); 
    mainChatLayout.setId((int)System.currentTimeMillis()); 
    ChatText.setBackgroundDrawable(getResources().getDrawable(R.drawable.talk1)); 
    ChatText.setGravity(Gravity.RIGHT); // not working 
    ChatText.setText("some text"); 
    ChatText.setTextColor(Color.parseColor("#333333")); 
    ChatText.setPadding(0, 10, 20, 10); 
} 
else{ 
    ChatText = new TextView(main.this); 
    mainChatLayout.setId((int)System.currentTimeMillis()); 
    ChatText.setBackgroundDrawable(getResources().getDrawable(R.drawable.talk1)); 
    ChatText.setGravity(Gravity.LEFT); 
    ChatText.setText("Time: " + System.currentTimeMillis()); 
    ChatText.setTextColor(Color.parseColor("#333333")); 
    ChatText.setPadding(20, 10, 0, 10); 
} 
mainChatLayout.addView(ChatText, tv); 
MainLayout.addView(mainChatLayout, lp); 
recent = mainChatLayout; 

回答

1
  LinearLayout ll= /// Find Your Layout Using ID 
      TextView text=new TextView(this); 

現在設置結構。 LayoutParam將是TextView的父類型

text.setLayoutParams(new LinearLayout.LayoutParams(100,100)); 
    ll.addView(text); 
+0

你解決了嗎? – Sameer 2012-02-15 12:16:00

相關問題