andriod创建用户界面(1)
一.UI知识Activity
将UI放在一个Activity上面,可以在Activity的onCreate方法中添加UI。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
如果想得到UI上的控件,可以通过findViewById方法查找,例如:
ListView myListView = (ListView)findViewById(R.id.my_list_view);
二、UI布局
布局是对ViewGroup类的扩展,ViewGroup是对View的扩展。
下面是常见的布局类:
FrameLayout:通常是从左上角开始布局,不过如果多个子视图在同一个FrameLayout时,可能会出现重叠。
LinearLayout:按水平或垂直对齐每个子视图。有些像StackPanel。
RelativeLayout:定义子视图与其他视图或者屏幕边界的相对位置。
GridLayout:行列布局,在4.0引入。
2.1定义布局
实例LinearLayout:
<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:name="com.paad.todolist.NewItemFragment"
android:id="@+id/NewItemFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<fragment android:name="com.paad.todolist.ToDoListFragment"
android:id="@+id/TodoListFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
match_parent:显示内容所需最小尺度,如字体的高度。
wrap_content:使其充满,
三.To-Do-List实例
主要代码
//找到ListView以便使用Adapter,
//找到TextView以便监听其事件。
ListView myListView = (ListView)findViewById(R.id.myListView);
final EditText myEditText = (EditText)findViewById(R.id.myEditText);
// Create the Array List of to do items
final ArrayList<String> todoItems = new ArrayList<String>();
// Create the Array Adapter to bind the array to the List View
final ArrayAdapter<String> aa;
//使用系统的list_item作为ArrayAdapter的item项
aa = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
todoItems);
// Bind the Array Adapter to the List View
myListView.setAdapter(aa);
myEditText.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN)
if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER) ||
(keyCode == KeyEvent.KEYCODE_ENTER)) {
todoItems.add(0, myEditText.getText().toString());
aa.notifyDataSetChanged();
myEditText.setText("");
return true;
}
return false;
}
});
四、Fragment
其灵活的特点,可以让Fragment达到复用,多个Activity使用同一个Fragment。同时一个Activity可以有多个Fragment组成。
如果想在1.6以下版本,Activity必须继承FragmentActivity。
1.Fragment的生命周期
依赖于Activity。
下面给出所有事件

onAttach()方法获取Activity,onCreate()初始化Fragment,onCreateView()设置Fragment的布局。当Activity和Fragment创建完了激发onActivityCreated()方法。onStart()方法生命周期算是正式开始,像小孩子长成大人了一样。onResume()当活动生命周期时触发,比如结婚的时间触发。onPause()方法活动生命周期结束时,像结婚后要度蜜月一样 。onSaveInstanceState()在活动结束后要记录一下状态。以便回来继续处理。
onStop()方法,可见生命周期结束时调用。 onDestroyView()当Fragment的View分离时触发。onDestroy() 在生命周期结束时触发。onDetach是Fragment从Activity分离时发生。

2.FragmentManager
每个Activity都包括一个FragmentManager。用来访问Activity上面的Fragment,可以通过FragmentTransaction来添加替换。删除。
3、FragmentTransaction可通过FragmentManager.BeginTransaction()获取。
然后可以通过Transaction来将Fragment添加到Activity容器中。
xml代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/ui_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<FrameLayout
android:id="@+id/details_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
/>
</LinearLayout>
java代码:
public class MyFragmentActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Inflate the layout containing the Fragment containers
setContentView(R.layout.fragment_container_layout);
FragmentManager fm = getFragmentManager();
// Check to see if the Fragment back stack has been populated
// If not, create and populate the layout.
DetailsFragment detailsFragment =
(DetailsFragment)fm.findFragmentById(R.id.details_container);
if (detailsFragment == null) {
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.details_container, new DetailsFragment());
ft.add(R.id.ui_container, new MyListFragment());
ft.commit();
}
}
}
注意以上划红线的部分是先在Activity中添加两个View——FrameLayout。后面通过代码把Fragment放入到View中的。
4、查找Fragment
FragmentManager manager=getFragmentManager();
manager.findFragmentById(id);通常在有UI视图的情况适用
manager.findFragmentByTag(string);,在没哟UI视图的情况适用。
通常是把Fragment添加后有个id,然后才可以通过findFragmentById。
manager.beginTransaction().add(fragment, tag);
通常和后台Fragment使用。
5.添加到BackStack,当按back键时。会回滚操作。

6.设置动画
manager.beginTransaction().setCustomAnimations(enter, exit)();
7.Fragment和Activity之间的接口
可以通过Fragment的onAttach()来获得Activity的引用。也可以getActivity() 来获取Activity。
通常获得Activity是为了让Fragment属性变化时,来调用Activity的一些方法。所以可以在Fragment中定义指定的接口,然后让Activity来调用接口即可。
ListFragment的例子:
public class ToDoListFragment extends ListFragment {
}
FragmentManager fm = getFragmentManager();
ToDoListFragment todoListFragment =
(ToDoListFragment)fm.findFragmentById(R.id.TodoListFragment);
// Create the array list of to do items
todoItems = new ArrayList<String>();
// Create the array adapter to bind the array to the listview
aa = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
todoItems);
// Bind the array adapter to the listview.
todoListFragment.setListAdapter(aa);
andriod创建用户界面(1)的更多相关文章
- 【翻译】利用Qt设计师窗体在运行时创建用户界面(Creating a user interface from a Qt Designer form at run-time)
利用Qt设计师窗体在运行时创建用户界面 我们利用Calculator窗体例子中创建的窗体(Form)来展示当一个应用(application)已经生成后,是可以在其运行时产生与例子中相同的用户界面. ...
- 腾讯发布 Omix 1.0 - 用 JSX 或 hyperscript 创建用户界面
腾讯发布 Omix 1.0 - 用 JSX 或 hyperscript 创建用户界面 今天,腾讯正式开源发布 Omix 1.0, 让开发者使用 JSX 或 hyperscript 创建用户界面. Gi ...
- Python 创建用户界面之 PyQt5 的使用
之前给大伙介绍了下 tkinter,有朋友希望小帅b对其它的 Python GUI 框架也说道说道,那么今天就来说说 PyQt5 如何创建用户界面. 很多人学习python,不知道从何学起.很多 ...
- matlab中uicontrol创建用户界面控件
来源:https://ww2.mathworks.cn/help/matlab/ref/uicontrol.html?searchHighlight=uicontrol&s_tid=doc_s ...
- Python Django框架笔记(三):django工作方式简单说明和创建用户界面
(一) 说明 简单说明下django的工作方式,并举2个例子. (二) Django工作方式 假定我们有下面这些文件 ,这里在前2篇的基础上增加了 templates目录(存放html文件) 和s ...
- Andriod - 创建自定义控件
控件和布局的继承结构: 可以看到,我们所用的所有控件都是直接或间接继承自 View的,所用的所有布局都是直接或间接继承自 ViewGroup 的.View 是 Android 中一种最基本的 UI 组 ...
- 在 ASP.NET 中创建数据访问和业务逻辑层(转)
.NET Framework 4 当在 ASP.NET 中处理数据时,可从使用通用软件模式中受益.其中一种模式是将数据访问代码与控制数据访问或提供其他业务规则的业务逻辑代码分开.在此模式中,这两个层均 ...
- 十大开源的.NET用户界面框架 让GUI设计不再犯难
选择一款合适的GUI框架是.NET开发中比较重要但又很棘手的问题,因为用户界面相当于一款应用的"门面",直接面向用户.好的UI更能吸引用户,有时甚至成为决定一款应用成败的关键.下面 ...
- [译] 用 Swift 创建自定义的键盘
本文翻译自 How to make a custom keyboard in iOS 8 using Swift 我将讲解一些关于键盘扩展的基本知识,然后使用iOS 8 提供的新应用扩展API来创建一 ...
随机推荐
- 【Eclipse】Eclipse如何如何集成Tomcat服务器
需要的环境 下载和配置JDK 读者可参见JDK的安装与配置 下载和配置Tomcat 读者可参见Tomcat的下载和配置 下载Eclipse 读者可参见Eclipse官方网站 Eclipse 4.4.0 ...
- TCP详解 (1)
网络层: IP 被称为洲际协议,ICMP称为互联网控制报文协议 IGMP 为互联网组管理协议 传输层: 传输层的作用是把应用程序给他的任务划分为数据包,然后传递给下面的层: 应用层: 应用层的协 ...
- 豆瓣上9分以上的IT书籍-编程语言篇
我当要学习某些技术时,第一时间就是去找相关的书籍.而豆瓣读书是我主要的参考依据,主要是它的评分基本比较靠谱,对于技术书籍,一般来说评分在8分以上就是不错的书籍了,而达到9分就可以列入"必读& ...
- SharePoint自动化部署,利用SPSD工具包
目录 怎样使用SPSD 配置Environment XML文件 PowerShell激活Feature 上篇博客讲了利用PowerShell导出.导入AD中的Users.这篇介绍简单介绍一下SPSD ...
- unity, 在材质上指定render queue
材质球inspector面板在debug模式下可以看到Custom Render Queue一项: 其默认值为-1,表示使用相应shader的render queue设置. 也可以人为将其改为其它值, ...
- Android 开发日常积累
Android 集合 Android 开源项目分类汇总 扔物线的 HenCoder 高级 Android 教程 hencoder HenCoder:给高级 Android 工程师的进阶手册 Andro ...
- android 判断点击的位置是不是在指定的view上
private boolean inRangeOfView(View view, MotionEvent ev){ int[] location = new int[2]; view.getLocat ...
- jQuery函数继承 $.extend, $.fn.extend
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- U盘启动ubuntu 12.04
第一步: 下载U盘启动制作工具Universal-USB-Installer-1.9.5.9.exe http://www.pendrivelinux.com/downloads/Universal- ...
- 菜鸟学SSH(一)——Struts实现简单登录(附源码)
从今天开始,一起跟各位聊聊java的三大框架——SSH.先从Struts开始说起,Struts对MVC进行了很好的封装,使用Struts的目的是为了帮助我们减少在运用MVC设计模型来开发Web应用的时 ...