安卓:使用的setContentView多次不好而改变的布局? [英] Android: is using setContentView multiple times bad while changing layouts?

查看:209
本文介绍了安卓:使用的setContentView多次不好而改变的布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用的setContentView多次不好而改变布局?

is using setContentView multiple times bad while changing layouts?

有人说,这是不好的,他们从来不说为什么。

Some people say that it's bad and they never say why.

和有一些其他的事情来改变布局使用按钮?

and is there some other thing to change layout using button?

推荐答案

让我们来看看<一href="http://developer.android.com/reference/android/app/Activity.html#setContentView%28android.view.View%29"相对=nofollow> Android的文件的:

设置活动内容,以明确的观点。这种观点被直接放入活动的视图层次结构。

Set the activity content to an explicit view. This view is placed directly into the activity's view hierarchy.

因此​​,的setContentView 将覆盖布局,并将其与更换一个新的。通常情况下,你只需要在一次的onCreate做到这一点。从理论上讲,你可以做到这一点多,但它涉及到重新绘制整个布局,这可能需要一些时间。有几个选择,这取决于你想要什么:

So, setContentView will overwrite the layout, and replace it with a new one. Usually, you only want to do this once in onCreate. Theoretically, you could do it more, but it involves re-drawing the entire layout, and this could take some time. There are a few alternatives, depending on exactly what you want:

  1. ViewAnimator :这是用于显示快速动画有用的,如果要更改视图快速连续多次。
  2. 片段 - 而不是重新绘制整个视图,您可以切换出碎片。每个片段是一种小型的活动,和整体这将包含code好得多。
  3. 通行证意图参数 - 将信息传递到活动帮助它建立起来。第一活动并将信息传递到一个共同的第二活性,其知道如何基于关闭它接收来自第一活动的信息来设置本身上。
  1. ViewAnimator: This is useful for showing a quick animation, if you want to change the view multiple times in quick succession.
  2. Fragments- Instead of re-drawing the entire view, you can switch out fragments. Each fragment is a kind of mini activity, and overall this will contain the code much better.
  3. Pass Intent Arguments- Pass information to an activity to help it set up. The first activity passes information to a common second activity, which knows how to set itself up based off of the information it receives from the first activity.

至于你的具体应用,这里就是我会做:

As for your specific application, here's what I would do:

  1. 在每个波段遵循特定的布局。只有1,或者几下,可能的布局。
  2. 当乐队活动开始,适当的布局选择,并填充,知道里面有什么了。

借助 Android SDK中显示了如何从一个传递数据活动到另一个。刚刚通过的第二项活动从第一需要的数据,使用这样的事情:

The Android SDK shows how to pass data from one activity to another. Just pass the data that the second activity needs from the first, using something like this:

Intent intent=new Intent(...);
intent.putExtra("Album","Some Album")
startActivity(intent);

第二项活动将做到这一点:

The second activity will do this:

Intent intent=getIntent();
String albumName=intent.getExtraString("Album");
//Does something with albumName, maybe get a TextView and .setText()

这篇关于安卓:使用的setContentView多次不好而改变的布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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