碎片是一种微型的活动,一般由多个碎片合并到一个Activity中显示.营造一个更好的UI界面
简单实例:
步骤1
新建一个left_layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/left"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/Replace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Button" />
</LinearLayout>
注意事项:LinearLayout的方向垂直,否者控件无法水平居中.
新建一个实例类LeftFragment,目的是为了将组件放入到碎片当中,实例化碎片控件
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_layout,container,false);
return view;
}
}
然后,新建一个right_layout和RightFragment.方式与以上相同,只需改一下参数即可.
步骤2
在主活动的Layout中,加入fragment控件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<fragment
android:id="@+id/left"
android:name="com.example.xun.fragment.leftFragment"//通过名字来查找相对应的碎片类,并实例化到控件中显示
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
</fragment>
<fragment
android:id="@+id/left"
android:name="com.example.xun.fragment.rightFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
</fragment>
</LinearLayout>
注意事项:一定要给控件加Id,否者运行APK则会应用崩溃.
动态加载
通过点击按钮,替换右边的碎片
先生成一个Another.layout及相应实例类,方式与步骤1一样.
在主活动类中,实现一个View.onClickListener并复写onClick方法.实现监听按钮
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.Replace);//找到空间
button.setOnClickListener(this);//给控件一个监听事件
replaceFragment(new rightFragment());//初始化生成right碎片
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.Replace:
replaceFragment(new Another());//生成Another碎片,及更新了右边的碎片
break;
}
}
private void replaceFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.right_layout, fragment);
transaction.commit();
}//具体实现动态方法.
}
修改住活动布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<fragment
android:id="@+id/lgf"
android:name="com.example.xun.fragment.leftFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
</fragment>
<FrameLayout
android:id="@+id/right_layout"//对应main活动里的,动态生成方法.
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
</FrameLayout>
</LinearLayout>