Android常用控件之Fragment仿Android4.0设置界面
Fragment是Android3.0新增的概念,是碎片的意思,它和Activity很相像,用来在一个Activity中描述一些行为或部分用户界面;使用多个Fragment可以在一个单独的Activity中建立多个UI面板;Fragment必须被嵌入到Activity中,所的生命周期就跟Activity一样。在Android4.0的设置界面就是左边显示列表,右边显示对应的详细信息,先看下做出来的效果图
这个程序分横竖屏显示,横屏是在一页显示,竖屏分两页显示
首先定义程序中要用到的数据Data.java
package com.example.fragmentdemo;
public final class Data {
// 标题
public static final String[] TITLES = { "声音", "显示", "存储", "电池", "应用程序",
"帐户与同步", "位置服务", "语言和输入法", "备份和重置", "日期与时间" };
// 详细内容
public static final String[] DETAIL = { "显示声音模式", "显示亮度、壁纸、休眠、自动旋转屏幕、字体大小",
"显示机器上SD卡的大小,总容量大小", "显示当前电量", "显示当前安装的应用程序", "显示帐号信息与同步信息",
"显示Google的位置服务、GPS", "显示当前语言信息", "显示备份的数据", "显示当前时间与日期" };
}
定义一个类继承于ListFragment用来显示左边的列表信息
package com.example.fragmentdemo; import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView; public class MyListFragment extends ListFragment {
boolean dualPane; // 是否在一屏上同时显示列表和详细内容 如果为真是横屏,否则竖屏
int curCheckPosition = 0; // 当前选择的索引位置 @Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
System.out.println("MyListFragment-----------onActivityCreated");
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_checked, Data.TITLES)); // 为列表设置适配器 View detailFrame = getActivity().findViewById(R.id.detail); // 获取布局文件中添加的FrameLayout帧布局管理器
dualPane = detailFrame != null
&& detailFrame.getVisibility() == View.VISIBLE; // 判断是否在一屏上同时显示列表和详细内容 if (savedInstanceState != null) {
curCheckPosition = savedInstanceState.getInt("curChoice", 0); // 更新当前选择的索引位置
} if (dualPane) { // 如果在一屏上同时显示列表和详细内容
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); // 设置列表为单选模式 CHOICE_MODE_SINGLE
showDetails(curCheckPosition); // 显示详细内容
}
} // 重写onSaveInstanceState()方法,保存当前选中的列表项的索引值
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("curChoice", curCheckPosition);
System.out.println("MyListFragment-----------onSaveInstanceState");
} // 重写onListItemClick()方法
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
System.out
.println("MyListFragment-----------onListItemClick position = "
+ position);
showDetails(position); // 调用showDetails()方法显示详细内容
} void showDetails(int index) {
curCheckPosition = index; // 更新保存当前索引位置的变量的值为当前选中值 if (dualPane) { // 当在一屏上同时显示列表和详细内容时 getListView().setItemChecked(index, true); // 设置选中列表项为选中状态 DetailFragment details = (DetailFragment) getFragmentManager()
.findFragmentById(R.id.detail); // 获取用于显示详细内容的Fragment
if (details == null || details.getShownIndex() != index) { // 如果如果
details = DetailFragment.newInstance(index); // 创建一个新的DetailFragment实例用于显示当前选择项对应的详细内容 // 要在activity中管理fragment, 需要使用FragmentManager
FragmentTransaction ft = getFragmentManager()
.beginTransaction();// 获得一个FragmentTransaction的实例
ft.replace(R.id.detail, details); // 替换原来显示的详细内容
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); // 设置转换效果
ft.commit(); // 提交事务
} } else { // 在一屏上只能显示列表或详细内容中的一个内容时 // 使用一个新的Activity显示详细内容
Intent intent = new Intent(getActivity(),
MainActivity.DetailActivity.class); // 创建一个Intent对象
intent.putExtra("index", index); // 设置一个要传递的参数
startActivity(intent); // 开启一个指定的Activity
}
}
}
右边显示详细信息的Fragment
package com.example.fragmentdemo; import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.TextView; public class DetailFragment extends Fragment {
// 创建一个DetailFragment的新实例,其中包括要传递的数据包
public static DetailFragment newInstance(int index) {
DetailFragment f = new DetailFragment(); // 将index作为一个参数传递
Bundle bundle = new Bundle(); // 实例化一个Bundle对象
bundle.putInt("index", index); // 将索引值添加到Bundle对象中
f.setArguments(bundle); // 将bundle对象作为Fragment的参数保存
return f;
} public int getShownIndex() {
return getArguments().getInt("index", 0); // 获取要显示的列表项索引
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
} ScrollView scroller = new ScrollView(getActivity()); // 创建一个滚动视图
TextView text = new TextView(getActivity()); // 创建一个文本框对象 text.setPadding(10, 10, 10, 10); // 设置内边距
scroller.addView(text); // 将文本框对象添加到滚动视图中
text.setText(Data.DETAIL[getShownIndex()]); // 设置文本框中要显示的文本
return scroller;
}
}
在主Activity中
package com.example.fragmentdemo; import android.os.Bundle;
import android.app.Activity;
import android.content.res.Configuration; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
System.out.println("MainActivity-----------onCreate");
} // 创建一个继承Activity的内部类,用于在手机界面中,通过Activity显示详细内容
public static class DetailActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 判断是否为横屏,如果为横屏,则结束当前Activity,准备使用Fragment显示详细内容
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
System.out.println("MainActivity-----DetailActivity------onCreate");
finish(); // 结束当前Activity
return;
} if (savedInstanceState == null) { //
// 在初始化时插入一个显示详细内容的Fragment
DetailFragment details = new DetailFragment();// 实例化DetailFragment的对象
details.setArguments(getIntent().getExtras()); // 设置要传递的参数
getFragmentManager().beginTransaction()
.add(android.R.id.content, details).commit(); // 添加一个显示详细内容的Fragment
System.out.println("MainActivity-----DetailActivity------onCreate--savedInstanceState == null");
}
}
} }
由于横竖屏的需要,定义两个布局文件
layout->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"
android:baselineAligned="false"> <fragment class="com.example.fragmentdemo.MyListFragment"
android:id="@+id/titles"
android:layout_weight="1"
android:layout_width="0px"
android:layout_height="match_parent" /> </LinearLayout>
layout_land->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"
android:baselineAligned="false"> <fragment class="com.example.fragmentdemo.MyListFragment"
android:id="@+id/titles"
android:layout_weight="1"
android:layout_width="0px"
android:layout_height="match_parent" /> <FrameLayout android:id="@+id/detail"
android:layout_weight="2"
android:layout_width="0px"
android:layout_height="match_parent"
android:background="?android:attr/detailsElementBackground" /> </LinearLayout>
示例代码
Android常用控件之Fragment仿Android4.0设置界面的更多相关文章
- 编写Java程序,使用Swing布局管理器和常用控件,实现仿QQ登录界面
返回本章节 返回作业目录 需求说明: 使用Swing布局管理器和常用控件,实现仿QQ登录界面 实现思路: 创建登录界面的类QQLogin,该类继承父类JFrame,在该类中创建无参数的构造方法,在构造 ...
- Android4.0设置界面改动总结(三)
Android4.0设置界面改动总结大概介绍了一下设置改tab风格,事实上原理非常easy,理解两个基本的函数就可以: ①.invalidateHeaders(),调用此函数将又一次调用onBuild ...
- Android4.0设置界面改动总结(二)
今年1月份的时候.有和大家分享给予Android4.0+系统设置的改动:Android4.0设置界面改动总结 时隔半年.回头看看那个时候的改动.事实上是有非常多问题的,比方说: ①.圆角Item会影响 ...
- Android常用控件及对应Robotium API
最近发现Android控件不熟悉,看Robotium的API都费劲. 常用Android控件: 控件类型 描述 相关类 Button 按钮,可以被用户按下或点击,以执行⼀个动作 Button Text ...
- 常用的基本控件 android常用控件
1.TextView:(文本框):不能编辑 android:textColor="@color/tv_show_color" 字体颜色 android:textSize ...
- Android常用控件
Android 中使用各种控件(View) DatePicker - 日期选择控件 TimePicker - 时间选择控件 ToggleButton - 双状态按钮控件 EditText - 可编辑 ...
- Android常用控件之GridView使用BaseAdapter
我们可以为GridView添加自定义的Adapter,首先看下用自定义Adapter的显示效果 在布局文件main.xml文件中定义一个GridView控件 <RelativeLayout xm ...
- Android常用控件之RatingBar的使用
RatingBar控件比较常见就是用来做评分控件,先上图看看什么是RatingBar 在布局文件中声明 <?xml version="1.0" encoding=" ...
- android常用控件的使用方法
引言 xml很强大 TextView <TextView android:id="@+id/text_view" android:layout_width="mat ...
随机推荐
- Delph控制台(Console)程序添加图标和版权信息
Delphi创建控制台(Console)程序默认是无法添加图标和版权的.经过仔细的对比窗体程序与控制台程序源码,发现窗体程序的工程文中,在uses结束begin开始的地方有一句如下代码:{$R *.r ...
- WAMP环境搭建步骤
在d盘创建myServer文件夹 然后apache2.2 mysql php-5.3.5 1 安装apache2.2 2 安装php-5.3.5 3 apache与php环境的整合 1)在httpd ...
- Nutch环境搭建
1. 环境准备 HOST:Ubuntu12.04LTS JDK: jdk-7u45-linux-i586.rpm Nutch:apache-nutch-1.7-bin.tar.gz Solr:solr ...
- ACM学习-POJ-1003-Hangover
菜鸟学习ACM,纪录自己成长过程中的点滴. 学习的路上,与君共勉. ACM学习-POJ-1003-Hangover Hangover Time Limit: 1000MS Memory Limit ...
- Jenkins的安全控制
在默认配置下,Jenkins是没有安全检查的.任何人都可以以匿名用户身份进入Jenkins,设置Jenkins和Job,执行build操作.但是,Jenkins在大多数应用中,尤其是暴露在互联网的应用 ...
- poj 3253 Fence Repair(模拟huffman树 + 优先队列)
题意:如果要切断一个长度为a的木条需要花费代价a, 问要切出要求的n个木条所需的最小代价. 思路:模拟huffman树,每次选取最小的两个数加入结果,再将这两个数的和加入队列. 注意priority_ ...
- OC基础14:使用文件
"OC基础"这个分类的文章是我在自学Stephen G.Kochan的<Objective-C程序设计第6版>过程中的笔记. 1.对于NSFileManager类,文件 ...
- 网站服务管理系统wdcp简介及功能特性
WDCP是WDlinux Control Panel的简称,是一套用PHP开发的Linux服务器管理系统以及虚拟主机管理系统,,旨在易于使用Linux系统做为我们的网站服务器,以及平时对Linux服务 ...
- 小米手机与魅族的PK战结果 说明了什么
我国电子商务面临的问题,淘宝退出百度无疑是一个遗憾.当在网上购物时.用户面临的一个非常大的问题就是怎样在众多的站点找到自己想要的物品,并以最低的价格买到.自从淘宝退出百度.建立自己的搜索引擎后,广大消 ...
- 如何用SQL SERVER 2005连接SQL SERVER 2008
原先使用sql server 2005数据库,后来由于工作需要升级为sql server 2008 开发版,升级过程很简单,基本没有什么问题 下面主要说说,如何使用sql server 2005 st ...