Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
534 views
in Technique[技术] by (71.8m points)

java - Clone textview to append it to a ViewGroup

I have a ViewGroup defined in XML with a view inside, at onCreate time I'd like to have a variable of those.
I don't want to go through the hassle of using a listview+adapter cause its clearly overkill as I know the list won't change since onCreate()
This is more or less the code I'd like to have.

TextView mytextview = myViewGroup.findViewById(R.id.mytext);

for(String test : strings){
  mytextview = mytextview.clone();
  mytextview.setText(test);
  myViewGroup.addView(mytextview);
}

But it is not working.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Maybe use an inflater, and put the textview in an external layout file:

View v = LayoutInflater.from(this).inflate(R.layout.textview_include, null);
viewGroup.addView(v);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...