克隆 textview 以将其附加到 ViewGroup [英] Clone textview to append it to a ViewGroup
本文介绍了克隆 textview 以将其附加到 ViewGroup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个用 XML 定义的 ViewGroup,里面有一个视图,在 onCreate 时间我想要一个变量.
我不想经历使用列表视图+适配器的麻烦,因为它显然是矫枉过正,因为我知道自 onCreate()
后列表不会改变这或多或少是我想要的代码.
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);
}
但它不起作用.
推荐答案
也许可以使用充气器,并将 textview 放在外部布局文件中:
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);
这篇关于克隆 textview 以将其附加到 ViewGroup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文