android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></FrameLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.75dp"
android:background="#999999" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:id="@+id/one"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="页面一"
android:textSize="30sp" />
<View
android:layout_width="0.75dp"
android:layout_height="match_parent"></View>
<TextView
android:id="@+id/two"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="页面二"
android:textSize="30sp" />
</LinearLayout>
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private FrameLayout content;
private Fragment01 fragment01;
private Fragment02 fragment02;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1.初始化界面
initViews();
}
private void initViews() {
TextView one = findViewById(R.id.one);
TextView two = findViewById(R.id.two);
content = findViewById(R.id.content);
one.setOnClickListener(this);
two.setOnClickListener(this);
//3.创建Fragment,添加到容器。
fragment01 = new Fragment01();
fragment02 = new Fragment02();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.content, fragment01);
transaction.commit();//事务每次只能提交一次
}
//2.点击事件
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.one:
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.content, fragment01);
transaction.commit();//事务每次只能提交一次
break;
case R.id.two:
FragmentManager fm1 = getSupportFragmentManager();
FragmentTransaction transaction1 = fm1.beginTransaction();
transaction1.replace(R.id.content, fragment02);
transaction1.commit();//事务每次只能提交一次
break;
}
}
}