1:什么是Fragment,Fragment是一种可以嵌入在活动中的UI片段,能够让程序更加合理和充分地利用大屏幕的空间,出现的初衷是为了适应大屏幕的平板电脑,可以将其看成一个小型Activity,又称作Activity片段。
2:Fragment依赖于Activity,不能独立存在,一个Activity可以有多个Fragment,一个Fragment可以被多个Activity重用,Fragment有自己的生命周期,并能接收输入事件,可以在Activity运行时动态地添加或删除Fragment
3:Fragment的生命周期
生命周期我们下次再来细说。
3:接下来就写一个比较简单的实例
(1)activity_main.xml
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.myapplication.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:gravity="center">
<TextView
android:id="@+id/text_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:text="Hello World!" />
</LinearLayout>
<fragment
android:id="@+id/fargment_id"
android:name="com.example.myapplication.FragmentButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_gravity="center"/>
(2)fragment_button.xml
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.myapplication.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:gravity="center">
<Button
android:id="@+id/button_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:text="click" />
</LinearLayout>
</LinearLayout>
(3)FragmentButton类
public class FragmentButton extends Fragment implements View.OnClickListener {
private Button button;
private TextView textView;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_button,null);
button = (Button) view.findViewById(R.id.button_id);
button.setOnClickListener(this);
//这里就是把view返回给MainActivity里的方法
return view;
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button_id:
//在Fragment中使用Activity中控件的方式
// 在当前的Fragment中调用getActivity方法获取依附着的那个Activity,
// 然后再用获取到的Activity去findViewById拿到你需要的控件对其操作就行了。
AppCompatActivity activity = (AppCompatActivity) getActivity();
textView = (TextView) activity.findViewById(R.id.text_id);
textView.setText("我是 fragment Button");
break;
default:
break;
}
}
}
(4)MainActivity类
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
5:运行结果
点击按钮
一、正则表达式概述 二、正则表达式在VBScript中的应用 三、正则表达式在VavaScr...
vbs:把一段文字中指定字符颜色变成红色的正则 functionc(Tstr,Word) Dimre Setre...
前言 相信大家都知道在IDE中代码的智能提示几乎都是标配,虽然一些文本编辑器也...
【排序算法】之lowb三人组冒泡、插入、选择 什么是lowb三人组 冒泡排序bubble so...
计算属性computed: 支持缓存,只有依赖数据发生改变,才会重新进行计算 不支持...
微信文件传输助手是微信电脑版与手机微信之间相互传输图片等文件的好工具,但很...
歌词编辑器 歌词编辑器 第一步:选择要播放的歌曲并播放 第二步:填写全部的歌词...
ADO对象: Connection Command Recordset Record Stream ASP支持的对象很多,可...
一石激起千层浪,继中国区浩浩荡荡的大裁员告一段落之后,甲骨文并未因此收起手...
本文将研究 ES6 的 for ... of 循环。 旧方法 在过去,有两种方法可以遍历 javas...