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 MainActivity extends FragmentActivity implements View.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);
}
}