2011-08-03 23 views
1

我需要能夠使用代碼將代碼添加到我的應用程序中,而不是使用XML或圖形界面... 我有一個imageview,但無法弄清楚如何修改它得到一個TextView(IM新到Android)...Android在java文件中添加Textview,而不是XML

這裏是我的ImageView代碼:

//Add view using Java Code 
ImageView imageView = new ImageView(AndroidAddViewActivity.this); 
imageView.setImageResource(R.drawable.icon); 
LayoutParams imageViewLayoutParams 
= new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
imageView.setLayoutParams(imageViewLayoutParams); 

mainLayout.addView(imageView); 

回答

4
TextView textView = new TextView(this); 
textView.setText("the text"); 
LayoutParams textViewLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
textView.setLayoutParams(textViewLayoutParams); 
mainLayout.addView(textView); 
+0

什麼可能是mainLayout變量(View)的數據類型? –

0

你可以用下面這樣做:

TextView textView = new TextView(this); 
textView.setText("the text"); 
相關問題