android UI:Fragment碎片
碎片(Fragment) 嵌入与活动中的UI片段,为了合理的分配布局而存在,这是我的简单理解。多用于兼顾手机与平板的UI,也适用于灵活高级的UI制作。
Demo 简单的按键切换两片不同的Demo
新建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_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Button"
/> </LinearLayout>
新建right_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#00ff00"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:text="This is right fragment"/>
</LinearLayout>
新建another_right_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"
android:background="#ffff00">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:text="This is right fragment"/> </LinearLayout>
main_Activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<fragment
android:layout_width="0dp"
android:name="test.example.com.fragmenttest.LeftFragment"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/left_fragment"/>
<FrameLayout
android:id="@+id/right_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"></FrameLayout> </LinearLayout>
分别新建对应的类
package test.example.com.fragmenttest; import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; /**
* Created by hs769 on 2017/4/4.
*/ public class LeftFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view=inflater.inflate(R.layout.lift_fregment,container,false);
return view;
}
}
package test.example.com.fragmenttest; //import android.app.Fragment;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; /**
* Created by hs769 on 2017/4/4.
*/ public class RightFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view=inflater.inflate(R.layout.right_fragment,container,false);
return view;
}
}
package test.example.com.fragmenttest; import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; /**
* Created by hs769 on 2017/4/4.
*/ public class AnotherRightFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view=inflater.inflate(R.layout.another_right_fragment,container,false);
return view;
}
}
package test.example.com.fragmenttest; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; 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);
replaceFragment(new RightFragment());
} @Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button:
replaceFragment(new AnotherRightFragment());
break;
default:
break;
}
}
private void replaceFragment(Fragment fragment){
FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction transaction=fragmentManager.beginTransaction();
transaction.replace(R.id.right_layout,fragment);
transaction.commit();
}
}

LeftFragment,RightFragment和another_Right_Fragment这三个类分别extends(继承)Fragment类,这是一个关键,因为有两个包中含有Fragment,建议选择android.support.v4.app.Fragment
如果包选择不一样会出现如下错误(MainAcitvity.java),如图更改即可:找到出问题的类,更换包,完毕



最终效果实现点击button切换碎片(下图为点击前后的变化,分别为两个Fragment)


android UI:Fragment碎片的更多相关文章
- Android之Fragment(碎片)方方面面
Fragment简介碎片(Fragment)是一种可以嵌入到活动当中的UI片段,它能让程序更加合理和充分的利用大屏幕的空间. Fragment的生命周期 它与Activity生命周期的关系: 可以看到 ...
- Android Fragment(碎片)的使用
简介 在Android中Fragment为一种可以嵌入活动中的UI片段.能让程序更加合理地利用大屏幕的空间. 使用方法 1.我们首先新建的一个onefragment.xml文件. <?xml v ...
- 【转】【Android UI设计与开发】第07期:底部菜单栏(二)Fragment的详细介绍和使用方法
原始地址:http://blog.csdn.net/yangyu20121224/article/category/1431917/1 由于TabActivity在Android4.0以后已经被完全弃 ...
- Android UI开发第三十篇——使用Fragment构建灵活的桌面
http://www.lupaworld.com/article-222973-1.html 当我们设计应用程序时,希望能够尽最大限度的适配各种设备,包括4寸屏.7寸屏. 10寸屏等等,Android ...
- Android UI开发第二十八篇——Fragment中使用左右滑动菜单
Fragment实现了Android UI的分片管理,尤其在平板开发中,好处多多.这一篇将借助Android UI开发第二十六篇——Fragment间的通信. Android UI开发第二十七篇——实 ...
- Android中Fragment与Activity之间的交互(两种实现方式)
(未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...
- Fragment碎片
布局文件中添加碎片 1.在onCteate()方法中调用inflater.inflate()加载Fragment布局 2.在xml的<fragment>中需要显示指明碎片名称(androi ...
- Android入门(六)碎片
原文链接:http://www.orlion.ga/493/ 一.碎片 碎片(Fragment)是一种可以嵌入在活动当中的 UI片段,它能让程序更加合理和充分地利用大屏幕的空间,因而在平板上应用的非常 ...
- Android之Fragment学习笔记①
Android Fragment完全解析,关于碎片你所需知道的一切 一. 什么是FragmentFragment(碎片)就是小型的Activity,它是在Android3.0时出现的.Fragment ...
随机推荐
- Macro_Average和Micro_Average准则的选择标准
- [leetcode]50. Pow(x, n)求幂
Implement pow(x, n), which calculates x raised to the power n (xn). Example 1: Input: 2.00000, 10 Ou ...
- 如何禁用Chrome / Firefox /IE浏览器的Cookie
Firefox: 点击菜单按钮并点击选项 按钮. 选择 隐私 面板. 将“Firefox 将会:”这一项设置为 使用自定义历史记录设置. 取消 接受来自站点的 Cookie 选项 即可禁用 Cooki ...
- wget下载指定URL下的特定属性文件
例子:下载指定URL下的kernel开头的所有包 wget https://archives.fedoraproject.org/pub/fedora/linux/updates/28/Everyth ...
- Mybatis 中实体类的编写
一个实体类对应一个数据表 一个属性对应一个字段 默认情况下类名和属性名都采用 “下划线转驼峰” 的命名方式.但具体采用什么样的命名方式并不重要(方式一致即可),在后面使用这些对象的时候,可以通过 re ...
- zeromq示例代码
http://www.oschina.net/code/snippet_614253_55034 将zeromq代码使用vc集中到一个解决方案中 可编译可调调试 vc2015
- hibernate的Could not execute JDBC batch update错误原因及处理
http://blog.csdn.net/derpvailzhangfan/article/details/2332795\ 上述问题: 一设置关联 二包含关键字 三 映射文件设置 catalog=“ ...
- ABP框架系列之四十五:(Quartz-Integration-Quartz-集成)
Introduction Quartz is a is a full-featured, open source job scheduling system that can be used from ...
- 安装zookeeper(单机,伪集群)
1.登陆zookeeper官网下载 https://zookeeper.apache.org/ zookeeper-3.4.8.tar.gz 解压:tar -zxvf zookeeper-3.4.8. ...
- Java生成静态HTML文件
private static final String FILEPATH = "/opt/nginx/html/banner/"; private static final Str ...