2013-01-14 35 views
0

我主要佈局是這樣的:如何減少活動數量

~~~~~~~~~~ 
~~~~~~~~~~ 
---------- 
[][][][][] 

底側是固定的,只要一個按鈕被按下頂部側正在發生變化。頂部和底部使用2種不同的線性佈局,底部的每個按鈕使用不同的Activity。其實它正在工作,但我不確定它是否正確。我覺得我過度使用Activities

我應該在Activities來回這種設置嗎?還是有更好的方法來解決這個問題?

+0

好了,你可以創建不同的'Fragments',甚至是不同的'Views'。 –

+0

[http://developer.android.com/guide/components/fragments.html](http://developer.android.com/guide/components/fragments.html) 是的,這正是我所需要的。我知道應該有更好的方法。謝謝。 – wervdon

回答

0

答案由奇異給出:片段
http://developer.android.com/guide/components/fragments.html

甲片段表示行爲或在一個活動的用戶界面的一部分。您可以在單個活動中組合多個片段來構建多窗格用戶界面,並在多個活動中重用片段。您可以將片段看作活動的模塊化部分,該活動有其自己的生命週期,接收自己的輸入事件以及可以在活動運行時添加或刪除的活動(有點像可以使用的「子活動」在不同的活動中重用)。

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.ViewGroup; 

public class ArticleFragment extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.article_view, container, false); 
    } 
} 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 

<fragment android:name="com.example.android.fragments.HeadlinesFragment" 
      android:id="@+id/headlines_fragment" 
      android:layout_weight="1" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" /> 

<fragment android:name="com.example.android.fragments.ArticleFragment" 
      android:id="@+id/article_fragment" 
      android:layout_weight="2" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" /> 

import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 

public class MainActivity extends FragmentActivity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.news_articles); 
    } 
}