我創建了20頁的應用程序(從頁面到頁面),閱讀了一些信息後,我終於混淆了最佳解決方案。添加新的佈局,片段或新的Acitvity?
只創建新佈局並將它們添加到同一個MainActivity?
創建每次新的Activity +新佈局?
創建碎片並將它們添加到同一個MainActivity?
碎片和佈局之間的實際區別是什麼?
Java中用於添加新佈局/片段的代碼是什麼?
謝謝。
我創建了20頁的應用程序(從頁面到頁面),閱讀了一些信息後,我終於混淆了最佳解決方案。添加新的佈局,片段或新的Acitvity?
只創建新佈局並將它們添加到同一個MainActivity?
創建每次新的Activity +新佈局?
創建碎片並將它們添加到同一個MainActivity?
碎片和佈局之間的實際區別是什麼?
Java中用於添加新佈局/片段的代碼是什麼?
謝謝。
使用Fragments創建一個活動主頁,如下所示。
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.myproj.activity.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="wrap_content"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/container"
android:name="com.example.HomeFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
//設計烏爾片段
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:clickable="true">
.....
.....
.....
</LinearLayout>
// JAVA
public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
// call MyFragment page here
}
.....
.....
.....
}
//片段
public class MyFragment extends Fragment{
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstantState){
view = inflater.inflate(R.layout.fragment_page,container,false);
...
return view;
}
}
非常感謝。這裏是我的投票爲您的帖子Shunmugapriya。 – Melisa
很高興聽到..如果你得到答案,請點擊打勾..通過你的投票,其他人也可以從中受益 –
佈局只是XML資源定義如何佈局應該屏幕上看起來很像。碎片完全不同。將碎片視爲具有與之相關的佈局的小型活動。就像活動一樣,當創建片段時,他們膨脹一個佈局,然後渲染該佈局。然而碎片需要附加到一個活動中,沒有它的情況下它們不能存在。
對你來說,最好的辦法是到ViewPager
中使用的片段,這使您可以刷卡由左到右(反之亦然),並具有網頁
謝謝Isuru。這將允許保存來自每個頁面的數據(按下單選按鈕)並將其存儲在內存中直到第20頁? – Melisa
是的,包含所有片段的活動會將它們留在內存中,直到您明確要求它們分離。 – IsuruKusumal
很酷。非常感謝:)現在讓我們看看它是如何工作的。 – Melisa
這可能是你的興趣無窮多個HTTP ://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/ –
謝謝Majeed :) – Melisa