Fragment:碎片,
![](https://img.haomeiwen.com/i6619460/5e234dcc437d0591.png)
![](https://img.haomeiwen.com/i6619460/4b616470ba647b10.png)
- 准备碎片的布局
left
<?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">
<Button
android:id="@+id/left_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="button"/>
</LinearLayout>
right
<?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">
<TextView
android:id="@+id/right_textView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="20sp"
android:text="right_fragment"/>
</LinearLayout>
anthorRight
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="20sp"
android:text="another textView"/>
</LinearLayout>
- 加载布局文件
package com.example.bbw.fragmentdemo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by bbw on 2017/7/21.
*/
public class LeftFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.left_fragment,container,false);
return view;
}
}
其他类似
- activity_main.xml引入碎片布局文件
class:指定要加入的碎片
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.bbw.fragmentdemo.MainActivity">
<fragment
android:id="@+id/left_fragment"
class = "com.example.bbw.fragmentdemo.LeftFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<FrameLayout
android:id="@+id/right_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
FrameLayout用来作为右碎片布局的母布局(原谅找不到词)
- 点击按钮会触发右边碎片布局的替换分为以下几步
private void replaceFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.right_layout,fragment);
transaction.commit();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.left_button);
replaceFragment(new RightFragment());
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
replaceFragment(new AnotherRightFragment());
}
});
}
网友评论