Android应用程序UI设计(1)_布局
来源:互联网 发布:nginx书籍 编辑:程序博客网 时间:2024/05/31 13:14
1.Android UI元素简介
Android使用xml文件进行布局。在布局文件中体现了一些UI元素。
1.Android.view.View(视图)
View是最基础的一个类,几乎所有的高级UI组件都是继承View类,如TextView,EditText,Button...。
一个视图(View)在屏幕上占据了一个矩形区域,它负责渲染这块矩形区域,也可以处理这块区域发生的事件,并且可以设置这块区域是否可见,是否可以获取焦点等。
2.Adroid.view.ViewGroup(视图容器)
View的容器,负责对这些view布局,并且允许嵌套。这是一个抽象类。他的实现类有:
a)LinearLayout进行水平或垂直布局。将元素进行线性的布局。
b)RelativeLayout进行相对布局。根据元素的相对位置布局。注意的是,出于性能考虑,位置只能计算一次,所以,如果控件A依 赖B ,那么B必须在A的前面。
c)FrameLayout 窗体布局,所有元素都防止在屏幕左上方,如果有多个元素,将重叠。
3.LayoutParameter(布局参数)
决定View的显示属性,比如长度、宽度、位置。
2.控件的布局
案例:
使用linearLayout布局
布局文件为:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/bt_1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/frameLayout"/> <Button android:id="@+id/bt_2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/relativeLayout"/> <Button android:id="@+id/bt_3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/linearLayoutANDrelativeLayout"/> <Button android:id="@+id/bt_4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/tableLayout"/> </LinearLayout>
使用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/photo" android:src="@drawable/meinv"android:contentDescription="@string/meinv" android:layout_width="wrap_content" android:layout_height="wrap_content" /></FrameLayout>
linearLayout和RelativeLayout并用(视图容器的嵌套)
布局文件和Activity类代码
Left.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000" > <TextView android:id="@+id/tv_left_1" android:text="@string/left_1" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv_left_2" android:text="@string/left_2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/tv_left_1" /> </RelativeLayout>
Right.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" > <TextView android:id="@+id/tv_right_1" android:text="@string/right_1" android:layout_width="fill_parent" android:layout_height="50dip" /> <TextView android:id="@+id/tv_right_2" android:text="@string/right_2" android:layout_width="fill_parent" android:layout_height="50dip" android:layout_below="@id/tv_right_1" /> </RelativeLayout>
Activity代码
@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//实例化一个线性视图容器对象LinearLayout layout = new LinearLayout(this);layout.setOrientation(LinearLayout.HORIZONTAL);setContentView(layout);//实例化一个inflater对象,这个对象可以把xml解析成ViewLayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);//使用inflater把xml布局文件解析称ViewRelativeLayout left = (RelativeLayout)inflater.inflate(R.layout.left, null);RelativeLayout right = (RelativeLayout)inflater.inflate(R.layout.right, null);//实例化一个布局参数对象RelativeLayout.LayoutParams layoutParameter = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);//向视图容器中添加视图(在此处是视图容器,属于嵌套)//可见,除了在布局文件中对activity布局之外,也完全可以在activity类中布局layout.addView(left,100,100);layout.addView(right,layoutParameter);}
relativeLayout
布局文件为:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#888888" android:padding="10dip" > <TextView android:id="@+id/tv_input_name" android:text="@string/input_name" android:layout_height="wrap_content"android:layout_width="wrap_content" /> <EditText android:id="@+id/et_name" android:background="@android:drawable/editbox_background" android:layout_height="wrap_content"android:layout_width="fill_parent"android:hint="@string/input"android:layout_below="@id/tv_input_name" /> <Button android:id="@+id/bt_input_name_cancel" android:layout_height="wrap_content"android:layout_width="wrap_content"android:text="@string/cancel"android:layout_below="@id/et_name"android:layout_alignParentRight="true"android:layout_marginRight="10dip" /> <Button android:id="@+id/bt_input_name_ok" android:layout_below="@id/et_name" android:layout_height="wrap_content"android:layout_width="wrap_content"android:text="@string/ok"android:layout_toLeftOf="@id/bt_input_name_cancel"android:layout_alignTop="@id/bt_input_name_cancel"android:layout_marginRight="10dip" /> </RelativeLayout>
TableLayout
<?xml version="1.0" encoding="utf-8"?><TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:stretchColumns="1" > <TableRow > <TextView android:text="@string/name" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <EditText android:hint="@string/input" android:layout_width="fill_parent" android:layout_height="wrap_content" /></TableRow> <TableRow > <TextView android:text="@string/password" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <EditText android:hint="@string/input" android:layout_width="fill_parent" android:layout_height="wrap_content" /></TableRow> <TableRow > <Button android:text="@string/ok" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:text="@string/cancel" android:layout_width="fill_parent" android:layout_height="wrap_content" /></TableRow> </TableLayout>
- Android应用程序UI设计(1)_布局
- Android UI 设计-相对布局
- Android-UI布局1
- Android UI设计——布局方法
- 【Android】UI设计之界面布局
- Android继承式UI界面布局设计
- 【Android 设计】:启航_ UI概览
- Android工具之Hierarchy Viewer--分析应用程序UI布局
- android UI设计,android ui开发,android 页面设计,android页面布局
- Android布局属性android:clipToPadding的UI设计妙用
- Android布局属性android:clipToPadding的UI设计妙用
- DroidDraw UI设计软件开发Android GUI应用程序
- android UI布局设计 —— layout_weight浅见
- Android UI设计的FrameLayout与TableLayout布局
- Android初始UI界面的布局的简单设计
- Android传奇UI设计之协调者布局CoordinatorLayout
- 详解Android 自定义UI模板设计_由浅入深
- Android UI设计之布局-详细解析布局实现(线性布局)
- 黑客编程教程(十五)HTTP代理
- 数字,字符串 和集合
- 黑客编程教程(十六)线程插入技术
- java实现栈的反转
- hdu 1542 Atlantis
- Android应用程序UI设计(1)_布局
- 线程间通信
- asp.net 验证正则表达式集合-1
- 数组生成的所有集合
- HaspMap(jiava)
- build-helper-maven-plugin 配置多 source resource 文件
- java识别文件编码格式代码(无引用jar包,测试可行)
- UVA 10194 足球
- 深入理解Hadoop集群和网络