[Android新手学习笔记26]-Fragment简单例子
来源:互联网 发布:网络直播软件下载 编辑:程序博客网 时间:2024/06/18 09:00
简单例子:
创建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设备显示两列,在其他设备显示一列。其他限定符如下:
最小限定符:
在res目录下新建layout-sw600dp文件夹,只要大于600dpi都都使用。
0 0
- [Android新手学习笔记26]-Fragment简单例子
- [Android新手学习笔记27]-Fragment碎片生存周期
- [Android应用代码] Android Fragment简单例子
- [Android新手学习笔记25]-RecylerView简单应用
- Android Fragment学习笔记
- android fragment学习笔记
- 学习笔记 android fragment
- android Fragment 学习笔记
- Android学习笔记 - fragment
- Android学习笔记 Fragment
- 新手Android学习笔记 04
- Android Fragment学习笔记(1)———简单Fragment实现
- Pro Android学习笔记(三七):Fragment(2):基础小例子
- Pro Android学习笔记(三八):Fragment(3):基础小例子-续
- Pro Android学习笔记(三九):Fragment(4):基础小例子-续
- android学习笔记之Fragment
- Android 学习笔记13:Fragment
- Fragment生命周期---Android学习笔记
- 欢迎使用CSDN-markdown编辑器
- centos7下jenkins配置docker插件
- Linux服务管理
- Activiti 流程启动及节点流转源码分析
- linux指令(二)——用户、组和文件权限
- [Android新手学习笔记26]-Fragment简单例子
- git克隆项目和分支
- 8、解决Gradle dependencies compile jar包冲突、重复问题
- [Android新手学习笔记27]-Fragment碎片生存周期
- S3C2440启动程序运行过程
- iOS 中对 HTTPS 证书链的验证
- 操作系统引论 学习笔记
- linux中vim编辑器的简单配置
- 进程调度算法