Android 4学习(7):用户界面 - 基础
2013-10-18 17:10
302 查看
参考《Professional Android 4 Development》
View:View是所有UI元素,包括Layout在内,的父类。
View Groups:View的子类,实现了ViewManager接口。一个ViewGroup可以包含多个子View。
Fragmengts:用于封装UI的基本元素。Fragment有自己的layout配置文件,可以接收用户输入,可以方便地适配不同的屏幕。
Activity:用于表达android设备的屏幕,Activity类似于Web开发中的Form表单。View等UI元素只有绑到Activity中才可以被用户可见。
同样的,使用findViewById()方法可以通过View ID获取View对象:
http://developer.android.com/guide/topics/ui/declaring-layout.html#CommonLayouts
下面是一个Layout示例文件:
wrap_content将View的高(或宽)设置为能包裹控件内容的最小值,而match_parent则使View尽可能地填充父容器。
若将上面的layout添加到另一个Layout中,则会产生冗余(redundant),更好的解决方式是使用<merge>标签:
当使用<merge>标签的配置文件被嵌入到另一个文件中时,<merge>标签会被删掉。<merge>标签常和<include>一起使用,例如:
配置文件:
Android UI基本元素
下面这些概念是Android UI设计的基础,深入学习和理解它们是Android UI设计的基础:View:View是所有UI元素,包括Layout在内,的父类。
View Groups:View的子类,实现了ViewManager接口。一个ViewGroup可以包含多个子View。
Fragmengts:用于封装UI的基本元素。Fragment有自己的layout配置文件,可以接收用户输入,可以方便地适配不同的屏幕。
Activity:用于表达android设备的屏幕,Activity类似于Web开发中的Form表单。View等UI元素只有绑到Activity中才可以被用户可见。
Android UI基础
Android UI与Activity的绑定
最常用的方法是使用setContentView()将Layout ID或View对象传到Activity中,例如:@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView myTextView = new TextView(this); setContentView(myTextView); myTextView.setText(“Hello, Android”); }
同样的,使用findViewById()方法可以通过View ID获取View对象:
TextView myTextView = (TextView)findViewById(R.id.myTextView);
Layout简介
Layout是ViewGroup的子类,用于描述和管理Android UI的布局。Android自带了很多Layout,常用的包括FrameLayout,LinearLayout,RelativeLayout和GridLayout。关于Layout的更多信息,可以参考这个链接:http://developer.android.com/guide/topics/ui/declaring-layout.html#CommonLayouts
下面是一个Layout示例文件:
<?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=”wrap_content” android:text=”Enter Text Below” /> <EditText android:layout_width=”match_parent” android:layout_height=”wrap_content” android:text=”Text Goes Here!” /> </LinearLayout>
wrap_content将View的高(或宽)设置为能包裹控件内容的最小值,而match_parent则使View尽可能地填充父容器。
Layout优化
<merge>: 由于Layout可以nest(叠加?)使用,所以有时会出现冗余的Layout。一个常见的例子是使用FrameLayout创建一个单根的配置文件,例如:<?xml version=”1.0” encoding=”utf-8”?> <FrameLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”match_parent” android:layout_height=”match_parent”> <ImageView android:id=”@+id/myImageView” android:layout_width=”match_parent” android:layout_height=”match_parent” android:src=”@drawable/myimage” /> <TextView android:id=”@+id/myTextView” android:layout_width=”match_parent” android:layout_height=”wrap_content” android:text=”@string/hello” android:gravity=”center_horizontal” android:layout_gravity=”bottom” /> </FrameLayout>
若将上面的layout添加到另一个Layout中,则会产生冗余(redundant),更好的解决方式是使用<merge>标签:
<?xml version=”1.0” encoding=”utf-8”?> <merge xmlns:android=”http://schemas.android.com/apk/res/android”> <ImageView android:id=”@+id/myImageView” android:layout_width=”match_parent” android:layout_height=”match_parent” android:src=”@drawable/myimage” /> <TextView android:id=”@+id/myTextView” android:layout_width=”match_parent” android:layout_height=”wrap_content” android:text=”@string/hello” android:gravity=”center_horizontal” android:layout_gravity=”bottom” /> </merge>
当使用<merge>标签的配置文件被嵌入到另一个文件中时,<merge>标签会被删掉。<merge>标签常和<include>一起使用,例如:
<?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”> <include android:id=”@+id/my_action_bar” layout=”@layout/actionbar”/> <include android:id=”@+id/my_image_text_layout” layout=”@layout/image_text_layout”/> </LinearLayout>
使用ViewStub
ViewStub是View的子类,使用类似Lazy Load的方式加载,从而节省了系统资源。在代码中使用:// Find the stub View stub = findViewById(R.id. download_progress_panel_stub); // Make it visible, causing it to inflate the child layout stub.setVisibility(View.VISIBLE); // Find the root node of the inflated stub layout View downloadProgressPanel = findViewById(R.id.download_progress_panel);
配置文件:
<?xml version=”1.0” encoding=”utf-8”?> <FrameLayout “xmlns:android=http://schemas.android.com/apk/res/android” android:layout_width=”match_parent” android:layout_height=”match_parent”> <ListView android:id=”@+id/myListView” android:layout_width=”match_parent” android:layout_height=”match_parent” /> <ViewStub android:id=”@+id/download_progress_panel_stub” android:layout=”@layout/progress_overlay_panel” android:inflatedId=”@+id/download_progress_panel” android:layout_width=”match_parent” android:layout_height=”wrap_content” android:layout_gravity=”bottom” /> </FrameLayout>
相关文章推荐
- Android用户界面基础之Chronometer控件学习
- Android 4学习(7):用户界面 - 基础
- Android用户界面基础之TextView控件,EditText控件,Button控件的学习
- Android用户界面基础之CheckBox(复选按钮)、RadioButton(单选按钮)学习
- Android的用户界面基础之View的学习
- Android用户界面基础之ViewGroup学习
- Android基础知识学习之线程通信(Handler学习)
- android的基础学习:SQLite存储方式以及数据库操作
- Android基础学习之获取当前apk的版本号
- ANDROID基础学习笔记_3_登录和保存数据
- Android(OPhone) 学习笔记 - 代码基础
- Android基础学习【历史流程重走】 ---- Activity的生命周期
- android基础学习之蓝牙基础
- android基础学习8——文件存储
- Android基础学习【历史流程重走】 ---- 四大组件之Service
- 【android基础学习之五】——基础控件Menu,Dialog,ImageView,ImageButton
- android用户界面之Gallery3D学习资料汇总
- Android基础学习之RecylerView学习
- android SurfaceView绘制 重新学习--基础绘制
- Android基础学习3—————Android OS的扩展库支持