Android入门——UI(7)——Fragment
先上fragment静态加载的代码
<?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"> <TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:text="这是一个Fragment"
android:background="#0f0"/>
</LinearLayout>
fragment_index.xml
package com.ouc.wkp.ui1; import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; /**
* Created by wkp on 2016/8/25.
*/
public class FragmentIndex extends Fragment {//app包下3.0以后才可以使用 @Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_index,container);
return view;
}
}
FragmentIndex.java
注意继承的Fragment最好是
import android.support.v4.app.Fragment; 这样可以兼容早期的安卓api
<?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"> <fragment
android:id="@+id/fragment1"
android:name="com.ouc.wkp.ui1.FragmentIndex"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
fragment_demo.xml
注意fragment元素需要指定id
package com.ouc.wkp.ui1; import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity; /**
* Created by wkp on 2016/8/25.
*/
public class FragmentDemo extends FragmentActivity{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_demo);
}
}
FragmentDemo.java
然后是动态加载
<?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" android:background="#f00"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是一个Fragment,通过动态方式加载"
/>
</LinearLayout>
fragment_index.xml
package com.ouc.wkp.ui1; import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; /**
* Created by wkp on 2016/8/25.
*/
public class FragmentIndex extends Fragment {//app包下3.0以后才可以使用 @Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//别遗忘第三个false参数,否则会出现 java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
View view=inflater.inflate(R.layout.fragment_index,container,false);
return view;
}
}
FragmentIndex.java
注意代码中的那个注释
<?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="vertical"> <Button
android:id="@+id/btn_addd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增加" /> <Button
android:id="@+id/btn_deletee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="减少" /> <FrameLayout
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="200dp"></FrameLayout>
</LinearLayout>
fragment_demo.xml
package com.ouc.wkp.ui1; import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View; /**
* Created by wkp on 2016/8/25.
*/
public class FragmentDemo extends FragmentActivity{ FragmentIndex fragmentIndex; @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_demo); fragmentIndex=new FragmentIndex(); findViewById(R.id.btn_addd).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fm=getSupportFragmentManager();
FragmentTransaction ft=fm.beginTransaction(); ft.replace(R.id.frame_layout,fragmentIndex);
ft.commit();
}
}); findViewById(R.id.btn_deletee).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fm=getSupportFragmentManager();
FragmentTransaction ft=fm.beginTransaction(); ft.remove(fragmentIndex);
ft.commit();
}
});
}
}
FragmentDemo.java
效果图 点击增加出现红块 点击减少消失
Android入门——UI(7)——Fragment的更多相关文章
- Android入门——UI(8)——Fragment(2)
先演示一下如何在一个activity中放置两个Fragment,先定义两个Fragment <?xml version="1.0" encoding="utf-8& ...
- Android入门——UI(9)
SwipRefreshLayout下拉刷新控件 <?xml version="1.0" encoding="utf-8"?> <android ...
- android入门——UI(6)——ViewPager+Menu+PopupWindow
一.使用ViewPager开发新特性引导界面 <?xml version="1.0" encoding="utf-8"?> <Relative ...
- android入门——UI(5)
最近时间实在匆忙,博客的代码基本没有解释. 介绍ExpandableListView <?xml version="1.0" encoding="utf-8&quo ...
- android入门——UI(4)
GridView控件实现菜单 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xml ...
- android入门——UI(3)
Spinner控件 ListView控件 一.Spinner控件 点击Spinner会弹出一个包含所有可选值的dropdown菜单,从该菜单中可以为Spinner选择一个新值. 有两种指定数据源的 ...
- Android入门——UI(2)
介绍SeekBar拖动条控件.ProgressBar进度条控件.DatePicker日历控件.TimePicker时间控件 <?xml version="1.0" encod ...
- android入门——UI(1)
一.使用TextView ImageView Button EditView做出登录页面 <?xml version="1.0" encoding="utf-8&q ...
- Android - 用Fragments实现动态UI - 创建Fragment
你可以把fragment当作activity中的一个活动模块,它有自己的生命周期,自己接收输入消息,可以在activity运行的时候添加和删除(就像可以在其他activity中重用的"子ac ...
随机推荐
- 最全面 Nginx 入门教程 + 常用配置解析
转自 http://blog.csdn.net/shootyou/article/details/6093562 Nginx介绍和安装 一个简单的配置文件 模块介绍 常用场景配置 进阶内容 参考资料 ...
- java中读取程序运行时间
第一种是以毫秒为单位计算的. Java代码 //伪代码 long startTime=System.currentTimeMillis(); //获取开始时间 doSomeThing(); // ...
- ByteBuffer用法总结
转自:http://blog.csdn.net/mars5337/article/details/6576417 在NIO中,数据的读写操作始终是与缓冲区相关联的.读取时信道(SocketChanne ...
- Android的logcat命令详解
前言 欢迎大家我分享和推荐好用的代码段~~ 声明 欢迎转载,但请保留文章原始出处: CSDN:http://www.csdn.net ...
- Hibernate 、多表关联映射-组件关联映射(component)
组件关联映射可以将一些简小的数据与主题放在一个表中,例如firstName 和LastName这两个结合在一起可以组成一个名字,但是再分别将这两个再建一个表就不太合适了,这个时候可以用到组件关联映射: ...
- oracle转换数字到格式化字符串
问题描写叙述 oracle假设存储number(20,2)数据,0数据库中为0.00,2.1数据库中为2.10,3.88存储为3.88, 假设直接从数据库中取出相应显示为: 0 2.1 3.88 保留 ...
- Virtualbox 启动虚拟机报错以及扩展、显卡驱动安装
一.Virtualbox虚拟机启动报错,如图 预先估计是BIOS中的cpu Virtualtion虚拟化支持是disable,结果一看是enable. 接下来只好Google,找到了这么一个帖子:ht ...
- PO状态为“处理中”的处理方法
EBS中经常会出现PO提交审批后状态为“处理中”的情况,此时PO创建人无法打开,审批人也无法打开,工作流等查看也无异常,可以使用一下SQL处理再进行审批: --set serveroutput on ...
- 修改EBS R12 URL连接端口
(TEST环境8002端口)1.停止所有应用服务2.修改<SID>_<server>.xml配置文件(如test_vis.xml)cd $APPL_TOP/admin (如cd ...
- Java代码整理