[Android]使用ActivityGroup來切換Activity和Layout
一、效果圖
要求點選底部不同圖片按鈕切換不同的Activity,並在中間顯示Activity對應的ContentView。
二、 實現程式碼
2.1 layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:orientation="vertical"
android:layout_height="fill_parent">
<LinearLayout android:gravity="center_horizontal"
android:background="@drawable/myinfor2" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/cust_title" android:textColor="@android:color/white"
android:textSize="28sp" android:text="模組1" android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</LinearLayout>
<!-- 中間動態載入View -->
<ScrollView android:measureAllChildren="true" android:id="@+id/containerBody"
android:layout_weight="1" android:layout_height="fill_parent"
android:layout_width="fill_parent">
</ScrollView>
<LinearLayout android:background="@android:color/black"
android:layout_gravity="bottom" android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<!-- 功能模組按鈕1 -->
<ImageView android:id="@+id/btnModule1" android:src="@android:drawable/ic_dialog_dialer"
android:layout_marginLeft="7dp" android:layout_marginTop="3dp"
android:layout_marginBottom="3dp" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- 功能模組按鈕2 -->
<ImageView android:id="@+id/btnModule2" android:src="@android:drawable/ic_dialog_info"
android:layout_marginLeft="7dp" android:layout_marginTop="3dp"
android:layout_marginBottom="3dp" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- 功能模組按鈕3 -->
<ImageView android:id="@+id/btnModule3" android:src="@android:drawable/ic_dialog_alert"
android:layout_marginLeft="7dp" android:layout_marginTop="3dp"
android:layout_marginBottom="3dp" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:orientation="vertical"
android:layout_height="fill_parent">
<LinearLayout android:gravity="center_horizontal"
android:background="@drawable/myinfor2" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/cust_title" android:textColor="@android:color/white"
android:textSize="28sp" android:text="模組1" android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</LinearLayout>
<!-- 中間動態載入View -->
<ScrollView android:measureAllChildren="true" android:id="@+id/containerBody"
android:layout_weight="1" android:layout_height="fill_parent"
android:layout_width="fill_parent">
</ScrollView>
<LinearLayout android:background="@android:color/black"
android:layout_gravity="bottom" android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<!-- 功能模組按鈕1 -->
<ImageView android:id="@+id/btnModule1" android:src="@android:drawable/ic_dialog_dialer"
android:layout_marginLeft="7dp" android:layout_marginTop="3dp"
android:layout_marginBottom="3dp" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- 功能模組按鈕2 -->
<ImageView android:id="@+id/btnModule2" android:src="@android:drawable/ic_dialog_info"
android:layout_marginLeft="7dp" android:layout_marginTop="3dp"
android:layout_marginBottom="3dp" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- 功能模組按鈕3 -->
<ImageView android:id="@+id/btnModule3" android:src="@android:drawable/ic_dialog_alert"
android:layout_marginLeft="7dp" android:layout_marginTop="3dp"
android:layout_marginBottom="3dp" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
2.2 TestView.java
/**
* 使用ActivityGroup來切換Activity和Layout
* @author 農民伯伯
* @version 2010-9-7
*
*/
public class TestView extends ActivityGroup {
private ScrollView container = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 隱藏標題欄
requestWindowFeature(Window.FEATURE_NO_TITLE);
// 設定檢視
setContentView(R.layout.layout);
container = (ScrollView) findViewById(R.id.containerBody);
// 模組1
ImageView btnModule1 = (ImageView) findViewById(R.id.btnModule1);
btnModule1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
container.removeAllViews();
container.addView(getLocalActivityManager().startActivity(
"Module1",
new Intent(TestView.this, ModuleView1.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView());
}
});
// 模組2
ImageView btnModule2 = (ImageView) findViewById(R.id.btnModule2);
btnModule2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
container.removeAllViews();
container.addView(getLocalActivityManager().startActivity(
"Module2",
new Intent(TestView.this, ModuleView2.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView());
}
});
// 模組3
ImageView btnModule3 = (ImageView) findViewById(R.id.btnModule3);
btnModule3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
container.removeAllViews();
container.addView(getLocalActivityManager().startActivity(
"Module3",
new Intent(TestView.this, ModuleView3.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView());
}
});
}
}
* 使用ActivityGroup來切換Activity和Layout
* @author 農民伯伯
* @version 2010-9-7
*
*/
public class TestView extends ActivityGroup {
private ScrollView container = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 隱藏標題欄
requestWindowFeature(Window.FEATURE_NO_TITLE);
// 設定檢視
setContentView(R.layout.layout);
container = (ScrollView) findViewById(R.id.containerBody);
// 模組1
ImageView btnModule1 = (ImageView) findViewById(R.id.btnModule1);
btnModule1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
container.removeAllViews();
container.addView(getLocalActivityManager().startActivity(
"Module1",
new Intent(TestView.this, ModuleView1.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView());
}
});
// 模組2
ImageView btnModule2 = (ImageView) findViewById(R.id.btnModule2);
btnModule2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
container.removeAllViews();
container.addView(getLocalActivityManager().startActivity(
"Module2",
new Intent(TestView.this, ModuleView2.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView());
}
});
// 模組3
ImageView btnModule3 = (ImageView) findViewById(R.id.btnModule3);
btnModule3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
container.removeAllViews();
container.addView(getLocalActivityManager().startActivity(
"Module3",
new Intent(TestView.this, ModuleView3.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView());
}
});
}
}
程式碼說明:
a). ModuleView1、ModuleView2、 ModuleView3分別繼承自Activity。
b). 想動態改變標題可以通過cust_title獲取TextView進行設定。
本文轉自部落格園農民伯伯的部落格,原文連結:[Android]使用ActivityGroup來切換Activity和Layout,如需轉載請自行聯絡原博主。
相關文章
- Android——Activity切換炫酷動畫實現Android動畫
- Appium用driver.start_activity切換Activity報錯APP
- Android技術分享| Activity 過渡動畫 — 讓切換更加炫酷Android動畫
- Activity橫豎屏切換生命週期
- 使用ViewPager和TabLayout來實現滑動切換效果ViewpagerTabLayout
- oracle 19c使用dgmgrl來執行switchover和failover切換OracleAI
- Android基礎-Activity基本使用Android
- Linux– su和sudo 切換使用者Linux
- 快速切換至Kotlin for Android模式KotlinAndroid模式
- Android之android:theme設定在Application 和 Activity的區別AndroidAPP
- Android 9 Activity的載入和顯示Android
- app直播原始碼,自定義兩種Activity切換動畫實現APP原始碼動畫
- Coordinator Layout使用
- 20181205使用者切換
- CUDA和CUDNN版本切換DNN
- 給 Flutter 介面切換來點特效Flutter特效
- siebel切換資料來源【轉】
- 使用Bootstrap tab頁切換的使用boot
- Android Activity Deeplink啟動來源獲取原始碼分析Android原始碼
- Android 實現APP可切換多語言AndroidAPP
- 【Android開發入門教程】四.使用者介面之LayoutAndroid
- [Android]關閉所有Activity,開啟某個ActivityAndroid
- 使用Broker實現DG切換
- ubuntu 介面和終端切換Ubuntu
- Android Activity生命週期Android
- Android之Activity全面解析Android
- 仿寫Android的ActivityAndroid
- Android Activity的基本理解Android
- Android Activity那點事Android
- Android中Context、Activity、ApplicatioAndroidContextAPP
- Android開發教程 - 使用Data Binding(三)在Activity中的使用Android
- PyQt教程——程式語言切換(Qt Linguist和pylupdate的使用)QTNGUI
- 註解切換雙資料來源
- Android進階(九)Activity外掛化和VirtualApk分析AndroidAPK
- Android基本控制元件和Activity的基本應用Android控制元件
- Android | Activity和Fragment最全生命週期+發現大牛AndroidFragment
- 如何在MySQL資料庫中使用use來切換資料庫?MySql資料庫
- [譯] 從 Android Studio 切換至 D8 dexerAndroid
- 程式切換(上下文切換)