动态加载在Android上的一个活动多个布局 [英] Loading multiple layouts dynamically within one activity on android

查看:184
本文介绍了动态加载在Android上的一个活动多个布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的应用程序,它具有一组问题的SQLite表。在这里用户可以借此测试负载最初的第一个问题的活动,那么他们每次$下一个P $ PSS时,它设置问题和答案的TextView到下一行的SQLite表中的文本。

I have an SQLite table on my application which has a set of questions. The activity where the user can take this test loads initially the first question, then every time they press next, it sets the text of the question and answers textView to the next row on the SQLite table.

问题是,如果我希望用户能够点击的答案,并移动到下一个问题,那么我想这个问题的答案仍然突出/检查,如果他们preSS的previous提问按钮 - 这意味着我认为最好的办法是将有单独的布局加载的活动中,为活动以显示一次的所有问题

The problem is, if I want the user to be able to click on the answer and move to the next question, then I want that answer to remain highlighted/checked if they press the previous question button - which means I think the best way would be to have all the questions loaded in separate layouts within the activity and for the activity to show one at a time.

是否可以循环并创建布局每次还是会杀死内存?

Is it possible to loop through and create a layout each time or will that kill the memory?

推荐答案

您不想有多种原因这样做。它很少是好主意,有多个布局活动。我不知道如何你的布局是假设一下,但你可以载入父母和孩子的意见动态是可能是你想要做的,如果你不知道每一次问题的数量是什么。

You don't want to do this for multiple reasons. It is rarely a good idea to have multiple layouts for one Activity. I don't know just how your layout is suppose to look but you can load parent and child views dynamically which is probably what you want to do if you don't know the number of questions each time.

您可以创建一个查看的东西,如

You can create a View with something like

TextView question = new TextView(this);

然后将其添加到布局 addView()

您可以有,比方说的LinearLayout 在一个单独的文件的膨胀,每次你需要一个新的问题,并设置查看里面相应。

You can have, say a LinearLayout in a separate file and inflate that each time you need a new question and set the Views inside it accordingly.

另一种选择,如果你知道有多少查看应该在你的布局将是XML来创建它们并使用能见度属性安卓知名度=隐形在XML中,然后将其更改为可见在Java中,当他们点击按钮

Another option, if you know how many Views should be in your layout would be to create them in xml and use the visibility property android:visibility="invisible" in xml then change it to visible in Java when they click the Button

textView.setVisibility(View.VISIBLE);

它很难给出很好的表率作用不知道你有什么,但我会建议寻找到这两个选项之一

Its hard to give much of an example without knowing exactly what you have but I would suggest looking into one of these two options

如果您有大量的布局,你可以考虑另外一个选项 S代表每个问题/答案或者希望他们是可重复使用的是创建单独的文件对每个和使用在<&包括GT; 标签包括每一个在你的主布局。你也可以使用能见度属性做这种方式,并有单独的文件。你只是想尽量避免调用的setContentView()多次,因为这将设置你所有的其默认值,它会导致混乱甚至造成更多的问题。

One other option that you may consider if you have large layouts for each question/answer or want them to be reusable is to create the separate files for each and use the <include> tag to include each one in your main layout. You could also use the visibility property doing it this way and have separate files. You just want to try and avoid calling setContentView() multiple times because that will set all of your Views to their default values and it can get messy causing even more problems.

这篇关于动态加载在Android上的一个活动多个布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆