android 使用Fragment 代替ActivityGroup
来源:互联网 发布:linux备份文件命令 编辑:程序博客网 时间:2024/06/03 10:03
1.创建activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <FrameLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" > </FrameLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp"> <RelativeLayout android:id="@+id/station_beacons_layout" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:orientation="vertical" > <TextView android:id="@+id/station_beacons" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="设置" android:textColor="#82858b" /> </LinearLayout> </RelativeLayout> <RelativeLayout android:id="@+id/scan_beancons_layout" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:orientation="vertical" > <TextView android:id="@+id/scan_beancons" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="联系人" android:textColor="#82858b" /> </LinearLayout> </RelativeLayout> </LinearLayout></LinearLayout>
2.在Class MainActivityextendsFragmentActivityimplementsView.OnClickListener onCreate 方法中添加
station_beacons_layout = findViewById(R.id.station_beacons_layout); scan_beancons_layout = findViewById(R.id.scan_beancons_layout); station_beacons = (TextView) findViewById(R.id.station_beacons); scan_beancons = (TextView) findViewById(R.id.scan_beancons); station_beacons_layout.setOnClickListener(this); scan_beancons_layout.setOnClickListener(this); fragmentManager = getSupportFragmentManager(); setTabSelection(0);
public void onClick(View v) { switch (v.getId()) { case R.id.station_beacons_layout: setTabSelection(0); break; case R.id.scan_beancons_layout: setTabSelection(1); break; } }
4.setTabSelection() 方法
<pre name="code" class="java">private void setTabSelection(int index) { clearSelection(); FragmentTransaction transaction = fragmentManager.beginTransaction(); hideFragments(transaction); switch (index) { case 0: station_beacons.setTextColor(Color.WHITE); if (stationBeaconsFragment == null) { stationBeaconsFragment = new StationBeaconsFragment(); transaction.add(R.id.content, stationBeaconsFragment); } else { transaction.show(stationBeaconsFragment); } break; case 1: scan_beancons.setTextColor(Color.WHITE); if (scanBeaconsFragment == null) { scanBeaconsFragment = new ScanBeaconFragment(); transaction.add(R.id.content, scanBeaconsFragment); } else { transaction.show(scanBeaconsFragment); } break; } transaction.commit(); } private void clearSelection() { station_beacons.setTextColor(Color.parseColor("#82858b")); scan_beancons.setTextColor(Color.parseColor("#82858b")); } private void hideFragments(FragmentTransaction transaction) { if (stationBeaconsFragment != null) { transaction.hide(stationBeaconsFragment); } if (scanBeaconsFragment != null) { transaction.hide(scanBeaconsFragment); } }
0 0
- android 使用Fragment 代替ActivityGroup
- 使用Fragment代替ActivityGroup,并解决屏幕旋转重叠问题
- Android Fragment应用实战,使用碎片向ActivityGroup说再见
- Android Fragment应用实战,使用碎片向ActivityGroup说再见
- Android Fragment应用实战,使用碎片向ActivityGroup说再见
- Android Fragment应用实战,使用碎片向ActivityGroup说再见
- Android Fragment应用实战,使用碎片向ActivityGroup说再见
- Android Fragment应用实战,使用碎片向ActivityGroup说再见
- Android Fragment应用实战,使用碎片向ActivityGroup说再见
- Android Fragment应用实战,使用碎片向ActivityGroup说再见
- Android Fragment应用实战,使用碎片向ActivityGroup说再见
- Android Fragment应用实战,使用碎片向ActivityGroup说再见
- Android Fragment应用实战,使用碎片向ActivityGroup说再见
- Android Fragment应用实战,使用碎片向ActivityGroup说再见
- Android Fragment应用实战,使用碎片向ActivityGroup说再见
- Android Fragment应用实战,使用碎片向ActivityGroup说再见
- Android Fragment应用实战,使用碎片向ActivityGroup说再见
- Android Fragment应用实战,使用碎片向ActivityGroup说再见
- MySQL concat函数的使用
- 欢迎使用CSDN-markdown编辑器
- spring的嵌套事务
- 关于DevOps(一)
- 终于搞定android驱动USB摄像头了!(zz)
- android 使用Fragment 代替ActivityGroup
- python第一次练习题
- MySQL CAST与CONVERT 函数的用法
- 权限管理——RBAC应用
- hdoj 3341 Lost's revenge 【AC自动机 + 变进制状态压缩dp】
- java 中的Exception RuntimeException 区别
- 比赛
- Visio Premium 2010密钥+破解激活方法
- 类中的const成员