简单例子:
创建layout/left_fragment.xml:
<?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/button"
android:layout_gravity="center_horizontal"
android:text="按钮"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
创建layout/right_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#00ff00"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text_view"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:text="这是右侧部分"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
修改activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/left_fragment"
android:name="top.xiexiaodong.fragmentdemo.LeftFramgment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
<fragment
android:id="@+id/right_fragment"
android:name="top.xiexiaodong.fragmentdemo.RightFragment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
</LinearLayout>
创建LeftFragment类,继承android.support.v4.app.Fragment:
public class LeftFramgment 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;
}
}
创建RightFragment类,继承android.support.v4.app.Fragment:
public class RightFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.right_fragment, container, false);
return view;
}
}
动态添加碎片:
创建anther_right_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#ffff00"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text_view"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:text="这是另一个右侧部分"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
修改activity_main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/left_fragment"
android:name="top.xiexiaodong.fragmentdemo.LeftFramgment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
<FrameLayout
android:id="@+id/right_fragment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
创建AntherFragment类:
public class AntherRightFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.anther_right_fragment, container, false);
return view;
}
}
修改MainActivity:
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.button);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
replaceFragment(new AntherRightFragment());
break;
default:
}
}
private void replaceFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.right_fragment, fragment);
transaction.commit();
}
}
添加到返回栈,效果,按Back键,返回上一个Fragment:
private void replaceFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.right_fragment, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
传入null即可。
碎片和活动之间进行通信:
活动获取碎片:
RightFragment rightFragment = (RightFragment) getSupportFragmentManager().findViewById(R.id.right_fragment);
碎片获取活动:
MainActivity mainActivity = (MainActivity) getActivity();
之后就可以使用碎片或者活动方法了。
使用限定符:
修改activity_main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/left_fragment"
android:name="top.xiexiaodong.fragmentdemo.LeftFramgment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
右键res文件夹,创建Android Resource File,File name:activity_main,和上面一致,Resource type:layout,Available qualifiers:Size,Chosen qualifiers(点击中间>>按钮):large,点击OK:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/left_fragment"
android:name="top.xiexiaodong.fragmentdemo.LeftFramgment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
<fragment
android:id="@+id/right_fragment"
android:name="top.xiexiaodong.fragmentdemo.RightFragment"
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="match_parent" />
</LinearLayout>
修改MainActivity:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
运行,这样,就可以在large设备显示两列,在其他设备显示一列。其他限定符如下:
屏幕特征 | 限定符 | 描述 |
大小 | small | 提供给小屏幕设备的资源 |
normal | 提供给中等屏幕设备的资源 | |
large | 提供给大屏幕设备的资源 | |
xlarge | 提供给超大屏幕设备的资源 | |
分辨率 | ldpi | 提供给低分辨率设备的资源(120dpi以下) |
mdpi | 提供给中等分辨率设备的资源(120dpi~160dpi) | |
hdpi | 提供给高分辨率设备的资源(160dpi~240dpi) | |
xhdpi | 提供给超高分辨率设备的资源(240dpi~320dpi) | |
xxhdpi | 提供给超超高分辨率设备的资源(320dpi~480dpi) | |
方向 | land | 提供给横屏设备的资源 |
port | 提供给竖屏设备的资源 |
最小限定符:
在res目录下新建layout-sw600dp文件夹,只要大于600dpi都都使用。