开发者

Android Add Textview in java file, not XML

开发者 https://www.devze.com 2023-03-25 20:55 出处:网络
I need to be able to add a textview to my app with code, 开发者_开发百科not using the XML or Graphic interface...

I need to be able to add a textview to my app with code, 开发者_开发百科not using the XML or Graphic interface... I have an imageview, but cannot figure out how to modify it to get a Textview (Im new to Android)...

Here is my imageview code:

//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);


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);


You can do so with the following:

TextView textView = new TextView(this);
textView.setText("the text");


You can add like

TextView nameHere = new TextView(this);
nameHere.setText("your text here");

This is just simple line of code you can modify it to do much more :)

0

精彩评论

暂无评论...
验证码 换一张
取 消