Fragment的创建与使用
来源:互联网 发布:mac战网该服务器 编辑:程序博客网 时间:2024/05/17 05:00
使用Fragment最简单的方式就是把Fragment当成最普通的控件,可以直接写在Activity的布局文件中。整个过程只需要两步:一是继承Fragment,重写onCreateView,决定Fragment的布局,二是在Activity布局文件中加入Fragment,与普通的View一样。
MainActivity.java
package com.sdutacm.viewpager.Fragment;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import com.sdutacm.viewpager.R;public class Main2Activity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout android:orientation="vertical" 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.sdutacm.viewpager.Fragment.MainActivity"> <fragment android:id="@+id/id_fragment_title" android:name="com.sdutacm.viewpager.Fragment.TitleFragment" android:layout_width="match_parent" android:layout_height="45dp" /> <fragment android:id="@+id/id_fragment_content" android:name="com.sdutacm.viewpager.Fragment.ContentFragment" android:layout_width="match_parent" android:layout_height="match_parent" /></LinearLayout>
TitleFragment.java
package com.sdutacm.viewpager.Fragment;import android.app.Fragment;import android.os.Bundle;import android.support.annotation.Nullable;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import com.sdutacm.viewpager.R;/** * Created by bummer on 2017/8/14. */public class TitleFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_title,container,false); return view; }}
fragment_title.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#EE30A7" tools:context="com.sdutacm.viewpager.Fragment.TitleFragment"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="新闻浏览器" android:textSize="28sp" /></LinearLayout>
ContentFragment.java
package com.sdutacm.viewpager.Fragment;import android.app.Fragment;import android.os.Bundle;import android.support.annotation.Nullable;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.Button;import android.widget.TextView;import com.sdutacm.viewpager.R;/** * Created by bummer on 2017/8/14. */public class ContentFragment extends Fragment{ private Button contentButton; private TextView textView; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_content,container,false); intiView(view); return view; } private void intiView(View view) { contentButton = (Button) view.findViewById(R.id.contentButton); textView = (TextView) view.findViewById(R.id.contentText); contentButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { textView.setText("中国羽毛队获得了奥运会冠军"); } }); }}
fragment_content.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" xmlns:tools="http://schemas.android.com/tools" tools:context="com.sdutacm.viewpager.Fragment.ContentFragment" > <TextView android:layout_width="match_parent" android:layout_height="100dp" android:id="@+id/contentText" android:text="@string/content" android:textSize="25sp" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/contentButton" android:text="@string/addNews" /></LinearLayout>
阅读全文
0 0
- Fragment的创建与使用
- Fragment简单的创建与使用
- Fragment与FragmentPagerAdapter的使用
- Fragment的概念与使用
- Fragment与ViewPager的使用
- Fragment的创建和基本使用
- 使用Fragment创建应用
- 静态创建使用Fragment
- Android Fragment的使用 三 Fragment与Activity交互
- [Andriod官方训练教程]使用Fragment创建一个动态的UI之创建一个Fragment
- Fragment的创建以及与activity的参数传递
- ActionBar 与Fragment的联合使用
- Fragment的概念与使用(一)--基本概念
- Fragment与ActionBar的结合使用
- Fragment的概念与使用(一)--基本概念
- Drawer Layout 与 Fragment的组合使用
- ViewPager与fragment配合使用的优势
- Fragment的解析与使用(一)
- 并发编程(三):同步容器和并发容器
- 判断字符串是否没有重复字符-LintCode
- 在 html页面中设置图片无法被选中
- ACM_线段树模板(区间更新)
- Hbase 存储读写机制
- Fragment的创建与使用
- Qt中为应用程序添加图片
- 解决UITapGestureRecognizer手势与UITableView的点击事件的冲突
- Win10安装you-get视频解析程序
- Qt中创建菜单和工具栏
- 利用反射实现一个Java文件书写多个Servlet,无框架
- Oracle常用函数
- c# 值类型和引用类型的区别,装箱,拆箱。
- 添加圆角的4种方法