问题
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?
回答1:
Let's take a look at the Android Documents:
Set the activity content to an explicit view. This view is placed directly into the activity's view hierarchy.
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:
- ViewAnimator: This is useful for showing a quick animation, if you want to change the view multiple times in quick succession.
- 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.
- 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:
- Each band follows a specific layout. There is only 1, or maybe a few, possible layouts.
- When the Band activity starts, the appropriate layout is chosen, and populated, knowing what's in there.
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()
回答2:
Yes this is bad, because it inflates your activity with your layout, and if your layout has a lot of views, it may take time.
To avoid that you should use a ViewAnimator, where you put all your layouts and you switch by showNext() and showPrevious(), i.e:
<ViewAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ViewAnimator"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout> </RelativeLayout>
<RelativeLayout> </RelativeLayout>
</ViewAnimator>
And in your code:
// Don't forget the setContentView
//
// Load the ViewAnimator and display the first layout
ViewAnimator va = (ViewAnimator) findViewById(R.id.ViewAnimator);
// Switch to the second layout
va.showNext();
// Add another layout at the third position
LinearLayout fooLayout = new LinearLayout(this);
va.addView(fooLayout, 3, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
回答3:
i cant show example because i'm waiting for answer to do it. okay i have the app of lyrics(it will show lyrics of band) and albums are new activities but i dont want many activities and thats why i want to make songs only layouts and change views with button press
It sounds like you're going about this the wrong way. If you want to change the UI an Activity
contains, then Fragment
s would be the better approach. There's a bit of a learning curve there, but it's good android design, and well documented.
Further, you seem to be confusing formatting and content. If you're displaying song lyrics, you don't need a new layout for each song. You just need to change the lyrics and keep them in the same activity. What you're doing is akin to creating a new web-browser for each web-page you intend to visit. Instead, find a way to store the lyrics and display them on a single activity (or in a fragment) to display those lyrics. The same would apply to each album: One activity would display the album cover in the corner (or as background), the title, release date, etc, as text, and then a list of songs below. The actual content of the TextView
s can change, but the layout ought to be the same.
来源:https://stackoverflow.com/questions/22227950/android-is-using-setcontentview-multiple-times-bad-while-changing-layouts