android – 动态添加视图而不显示视图

android – 动态添加视图而不显示视图,第1张

概述我正在使用以下代码在我的布局下为其子LinearLayout膨胀视图: LayoutInflater vi = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);View v = vi.inflate(R.layout.your_layout, null);// f 我正在使用以下代码在我的布局下为其子linearLayout膨胀视图:
LayoutInflater vi = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);VIEw v = vi.inflate(R.layout.your_layout,null);// fill in any details dynamically hereTextVIEw textVIEw = (TextVIEw) v.findVIEwByID(R.ID.a_text_vIEw); textVIEw.setText("your text");// insert into main vIEwVIEw insertPoint = findVIEwByID(R.ID.insert_point);((VIEwGroup) insertPoint).addVIEw(v,new VIEwGroup.LayoutParams(VIEwGroup.LayoutParams.WRAP_CONTENT,VIEwGroup.LayoutParams.WRAP_CONTENT));

但我无法看到我的观点.当我将屏幕旋转到横向视图时,它会显示出来.如何解决这个问题?

编辑:

这基本上就是我在做什么.我有一个带有两个选项卡的tabactivity.我使用一个应用程序全局,它存储一个标志和我的tabactivity的TabHost.我还在第二个标签活动中使用一个线程,该活动持续监视应用程序全局以监视标志.当我单击第一个选项卡中的按钮时,我将全局设置为true.第二个选项卡中的线程检查其值.当它成立时,我运行异步任务并使用ApplicationGlobal中的TabHost stired将当前显示窗口移动到第二个选项卡.

第二个标签代码:

public class ShowDownloadsActivity extends Activity { private List<String> List;ApplicationGlobal ap;String tempurl = "";linearLayout lly; LayoutInflater inflater;Context con;static int k = 0;static int tmp = 0;static int size = 0;VIEw insertPoint;VIEw v;public voID onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentVIEw(R.layout.shwds); con = this; v = LayoutInflater.from(this).inflate(R.layout.downloadListrow,null);insertPoint = (VIEw) findVIEwByID(R.ID.layout_vIDs);inflater = (LayoutInflater) con .getSystemService(Context.LAYOUT_INFLATER_SERVICE);ap = (ApplicationGlobal) getApplication();lly = (linearLayout) findVIEwByID(R.ID.layout_vIDs);Thread thread1 = new Thread() {    public voID run() {     try {      boolean flg = ap.getFlag();      if (flg) {     tempurl = ap.getUrl(); ap.setUrl("");   flg = false;   new downloadVIDeo().execute(); } } catch (Exception e) { e.printstacktrace(); } } };thread1.run(); }class downloadVIDeo extends AsyncTask<Hashtable<String,String>,String,String> { String resp = "";protected voID onProgressUpdate(String... values) { }@OverrIDeprotected voID onPreExecute() {// fill in any details dynamically here TextVIEw textVIEw = (TextVIEw) v.findVIEwByID(R.ID.siz);textVIEw.setText("your text");// insert into main vIEw addContentVIEw(v,new linearLayout.LayoutParams( linearLayout.LayoutParams.WRAP_CONTENT,linearLayout.LayoutParams.WRAP_CONTENT)); super.onPreExecute();}@OverrIDe protected String doInBackground(Hashtable<String,String>... prmss) { try {final int return resp;}@OverrIDeprotected voID onPostExecute(String result) { super.onPostExecute(result);} } }
解决方法 首先初始化要添加子视图的布局.

例如

linearLayout layout=(linearLayout)findVIEwByID(R.ID.your_root_layout);

然后像下面那样夸大你的布局

LayoutInflater mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);    VIEw v= (VIEw) mInflater.inflate(R.layout.your_layout,null);TextVIEw textVIEw = (TextVIEw) v.findVIEwByID(R.ID.a_text_vIEw);textVIEw.setText("your text");linearLayout.LayoutParams p = new linearLayout.LayoutParams(                linearLayout.LayoutParams.WRAP_CONTENT,linearLayout.LayoutParams.WRAP_CONTENT);layout.addVIEw(textVIEw,p);

希望这会帮助你.

总结

以上是内存溢出为你收集整理的android – 动态添加视图而不显示视图全部内容,希望文章能够帮你解决android – 动态添加视图而不显示视图所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1141084.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存