Android Add Textview in java file, not XML
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 :)
精彩评论