android布局范例
篇一:Android布局--九宫格示例
android布局--九宫格的实现
首先是main.xml的布局方式如下: 1. <?xml version="1.0" encoding="utf-8"?>
2. <!--主界面的布局-->
3. <RelativeLayout
4. xmlns:android="http://schemas.android.com/apk/res/android"
5. android:orientation="vertical"
6. android:layout_width="fill_parent"
7. android:layout_height="fill_parent"
8.
9. >
10. <RelativeLayout
11. android:id="@+id/MainActivityrlTwo"
12. android:layout_width="fill_parent"
13. android:layout_height="45dp"
14.
15. >
16.
17. </RelativeLayout>
18.
19. <GridView
20. android:id="@+id/MainActivityGrid"
21. android:layout_width="fill_parent"
22. android:layout_height="wrap_content"
23. android:numColumns="3"
24. android:columnWidth="50dp"
25. android:layout_below="@+id/MainActivityrlTwo"
26. android:layout_marginTop="5dp"
27. />
28.
29. <RelativeLayout
30. android:id="@+id/MainActivityrlThree"
31. android:layout_width="fill_parent"
32. android:layout_height="60dp"
33. android:layout_alignParentBottom="true"
34.
35. >
36. <TextView
37.android:id="@+id/tvLineBottom"
38.android:layout_width="fill_parent"
39.android:layout_height="wrap_content"
40.android:text="@string/line_default"
41./>
42. <Button
43.android:id="@+id/btmore_MainActivity"
44.android:layout_alignParentRight="true"
45.android:layout_alignParentBottom="true"
46.android:layout_width="wrap_content"
47.android:layout_height="wrap_content"
48.android:text="More"
49.
50./>
51. </RelativeLayout>
52.
53.</RelativeLayout>
------------------------------------------------------------------------------- 其次就是每一格九宫格的布局方式: 1. <?xml version="1.0" encoding="utf-8"?>
2. <!--九宫格每一格的布局-->
3. <LinearLayout
4. xmlns:android="http://schemas.android.com/apk/res/android"
5. android:orientation="vertical"
6. android:layout_width="fill_parent"
7. android:layout_height="fill_parent"
8. >
9. <ImageView
10. android:id="@+id/MainActivityImage"
11. android:layout_width="50dp"
12. android:layout_height="50dp"
13. android:layout_gravity="center_horizontal"
14. />
15. <TextView
16. android:id="@+id/MainActivityText"
17. android:layout_width="wrap_content"
18. android:layout_height="wrap_content"
19. android:layout_gravity="center_horizontal"
20. android:textSize="18sp"
21. android:lines="1"
22. android:layout_marginTop="8dp"
23. />
24.</LinearLayout>
25.
------------------------------------------------------------------------------- 最后就是adapter的编写: 1. public class ImageAdapter extends BaseAdapter {
2. private Context context;
3.
4. public ImageAdapter(Context context) {
5.this.context=context;
6. }
7.
8. private Integer[] images = {
9. //九宫格图片的设置
10.R.drawable.icon_1,
11.R.drawable.icon_2,
12.R.drawable.icon_3,
13.R.drawable.icon_4,
14.R.drawable.icon_5,
15.R.drawable.icon_6,
16.R.drawable.icon_7,
17.R.drawable.icon_8,
18.R.drawable.icon_9,
19.};
20.
21. private String[] texts = {
22.//九宫格图片下方文字的设置
23."记录支出",
24."记录收入",
25."账本管理",
26."类别管理",
27."查看图表",
28."收支对照",
29."记录心得",
30."新闻公告",
31."系统设置",
32.};
33.
34. //get the number
35. @Override
36. public int getCount() {
37. return images.length;
38. }
39.
40. @Override
41. public Object getItem(int position) {
42. return position;
43. }
44.
45. //get the current selector's id number
46. @Override
47. public long getItemId(int position) {
48. return position;
49. }
50.
51. //create view method
52. @Override
53. public View getView(int position, View view, ViewGroup viewgroup) {
54. ImgTextWrapper wrapper;
55. if(view==null) {
56.wrapper = new ImgTextWrapper();
57.LayoutInflater inflater = LayoutInflater.from(context);
58.view = inflater.inflate(R.layout.item, null);
59.view.setTag(wrapper);
60.view.setPadding(15, 15, 15, 15); //每格的间距
61. } else {
62.wrapper = (ImgTextWrapper)view.getTag();
63. }
64.
65. wrapper.imageView =
(ImageView)view.findViewById(R.id.MainActivityImage);
66. wrapper.imageView.setBackgroundResource(images[position]);
67. wrapper.textView =
(TextView)view.findViewById(R.id.MainActivityText);
68. wrapper.textView.setText(texts[position]);
69.
70. return view;
71. }
72.}
73.
74.class ImgTextWrapper {
75. ImageView imageView;
76. TextView textView;
77.
78.}
-------------------------------------------------------------------------------
篇二:Android布局--TableLayout示例
在Android开发中UI设计十分重要,当用户使用一个软件时,最先感受到的不是这款软件的功能是否强大,而是界面设计是否精致,用户体验是否良好。也可以这样说,有一个好的界面设计去吸引用户的使用,才能让更多的用户体验到软件功能的强大。需要说明的是,各个布局既可以单独使用,也可以嵌套使用,在实际应用中应灵活掌握。
TableLayout是指将子元素的位置分配到行或列中。Android的一个
TableLayout有许多TableRow组成,每一个TableRow都会定义一个Row。
TableLayout容器不会显示Row,Column,及Cell的边框线,每个Row拥有0个或多个Cell,每个Cell拥有一个View对象。
在使用tablelayout时,应注意每一个cell的宽度。
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<TextView android:id="@+id/lable1" android:text="用户名" android:textStyle="bold"
android:layout_width="55dip" android:gravity="center"/>
<EditText android:id="@+id/entry1" android:layout_width="250dip" android:layout_height="wrap_content"
/>
</TableRow>
<TableRow>
<TextView android:id="@+id/lable2" android:textStyle="bold" android:text="密码"
android:layout_width="55dip" android:gravity="center"/>
<EditText android:id="@+id/entry2"
android:layout_width="250dip" android:layout_height="wrap_content" android:password="true" android:scrollHorizontally="true"/> </TableRow>
</TableLayout>
篇三:Android_布局详解【图文】
Android 布局详解 【图文】
Android 布局是开发中非常重要的一个知识部分,它的布局分为以下几种:
Linear Layout:线性布局
Relative Layout:相对布局
Table Layout:表格布局
FrameLayout
AbsoluteLayout
Grid View:网格布局
Tab Layout:选项卡布局
List View:列表布局
一、Linear Layout
简单来说,直着排,横着排都可以,还可以嵌套,此布局运用的非常多。下面直接上示例代码及截图:
接下来,看一下布局XML文件:
<!--l version="<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_weight="1">
<TextView android:text="red" android:gravity="center_horizontal"
android:background="#aa0000" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:layout_weight="1" />
<TextView android:text="green" android:gravity="center_horizontal"
android:background="#00aa00" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:layout_weight="1" />
<TextView android:text="blue" android:gravity="center_horizontal"
android:background="#0000aa" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:layout_weight="1" />
<TextView android:text="yellow" android:gravity="center_horizontal"
android:background="#aaaa00" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:layout_weight=&q(来自:www.zaidian.cOm 书 业网:android布局范例)uot;1" />
</LinearLayout>
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_weight="1">
<TextView android:text="row one" android:textSize="15pt"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView android:text="row two" android:textSize="15pt"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView android:text="row three" android:textSize="15pt"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView android:text="row four" android:textSize="15pt"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
下面详细详解这些配置的含义:
LinearLayout 标签的常用属性 设置线性布局为水平方向
android:orientation="horizontal"
设置线性布局为垂直方向
android:orientation="vertical"
设置正比例分配控件范围
android:layout_weight="1"
设置控件显示位置,这里为水平居中
android:gravity="center_horizontal"
android:orientation="horizontal":定义方向水平或垂直(horizontal/vertical )
android:layout_width="fill_parent" :宽度填充满父控件的宽度
android:layout_height="fill_parent":宽度填充满父控件的高度
android:layout_weight="1":重量?可解释为权重,这是个什么意思呢,请看下图
我将这里的配置变了一下,
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1">
<TextView android:text="red" android:gravity="center_horizontal"
android:background="#aa0000" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" /> <TextView android:text="green" android:gravity="center_horizontal"
android:background="#00aa00" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="2" /> <TextView android:text="blue" android:gravity="center_horizontal"
android:background="#0000aa" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="3" /> <TextView android:text="yellow" android:gravity="center_horizontal" android:background="#aaaa00" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="4" /></LinearLayout>
可以看到我设置成了1,2,3,4,这四TextView显示的宽度不一样了,具体是怎么算的,
这个我们就不追究了,意思清楚就行,都设置为1则平分,否则数给的越大,占的位置就越多。 再看一下TextView的解释
<TextView android:text="red" android:gravity="center_horizontal"
android:background="#aa0000" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:layout_weight="1" />
android:text="red":要显示的内容
android:gravity="center_horizontal":显示内容的对齐方式
android:background="#aa0000" :背景色
android:layout_width="wrap_content":宽度,包括自己的内容的宽度
android:layout_height="fill_parent":高度,填充父控件的高度
android:layout_weight="1":权重
其实含义如果懂些CSS属性的话,还是蛮好懂的,布局跟Div有点类似
//类似一个外层DIV,里面的内容垂直布局android:orientation="vertical"
<LinearLayout xmlns:android="
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
//类似第一个子DIV,内容水平布局android:orientation="horizontal"
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_weight="1"></LinearLayout>
//类似第二个子DIV,内容垂直布局android:orientation="vertical"
<LinearLayout android:orientation="vertical"
篇四:Android常用布局
常用布局
Android中的布局包括线性布局,表格布局,相对布局,帧布局,绝对布局。下面分别对每个布局进行一个介绍。
在介绍android的布局管理器之前,有必要了解android平台下的控件类。首先要了解的是View类,该类为所有可视化控件的基类,主要提供了控件绘制和事件处理的方法。创建用户界面所使用的控件都继承自View,如TextView,Button,CheckBox等。
可以充当其他控件的容器。ViewGroup的子控件既可以是普通的View,也可以是ViewGroup。Android中的一些高级控件如Gally,GirdView等都继承自
ViewGroup。
一、线性布局
LinearLayout类简介
LinearLayout按照垂直或者水平的顺序依次排列子元素,每一个子元素都位于前一个元素之后。如果是垂直排列,那么将是一个N行单列的结构,每一行只会有一个元素,而不论这个元素的宽度为多少;如果是水平排列,那么将是一个单行N列的结构。如果搭建两行两列的结构,通常的方式是先垂直排列两个元素,每一个元素里再包含一个LinearLayout进行水平排列。
LinearLayout中的子元素属性android:layout_weight生效,它用于描述该子元素在剩余空间中占有的大小比例。加入一行只有一个文本框,那么它的默认值就为0,如果一行中有两个等长的文本框,那么他们的android:layout_weight值可以是同为1。如果一行中有两个不等长的文本框,那么他们的android:layout_weight值分别为1和2,那么第一个文本框将占据剩余空间的三分之二,第二个文本框将占据剩余空间中的三分之一。android:layout_weight遵循数值越小,重要度越高的原则。显示效果如下:
案例:
1. 在布局管理器中实现布局:
在eclipse中新建一个项目。首先打开项目文件夹下res/values目录下的string.xml,写入一下内容:
<?xml version=”1.0”encoding=”utf-8”?>
<resources>
<string name=”app_name”>LinearExample</string>
<string name=”button”>按钮 </string>
<string name=”add”>添加</string>
</resources>
说明:在string.xml中主要声明了程序中要用到的字符串资源,这样将所有字符串资源统一管理有助于提高程序的可读性及可维护性。
打开项目文件夹下的res/layout目录下的main.xml,将其中已有的代码替换为如下代码:
<?xml version=”1.0”encoding=”utf-8”?>
<LinearLayout xmlns:android=
android: orientation=” vertical”
android: layout_width=” fill_parent”
android: layout_height=” fill_parent”
android: id=” @+id/lla”
android: gravity =” right”>
<!--声明一个LinearLayout布局,并设置其属性-->
<Button
android:text=”@string/add”
android: id=” @+id/Button01”
android: layout_width=” wrap_content”
android: layout_height=” wrap_content”></Button>
<!--声明一个Button布局,并设置其id为Button 01-->
</ LinearLayout>
2. 在代码中实现布局中控件的绑定与监听:
打开项目的Activity文件LinearActivity.java,将其中已有的代码替换为如下的代码。
package wyf.jc;//声明包语句
import android.app.Activity; //引入相关类
import android.os.Bundle;//引入相关类
import android.view.View;//引入相关类
import android.widget.Button; //引入相关类
import android.widget. LinearLayout; //引入相关类
public class LinearActivity extends Activity{
int count=0; //计数器,记录按钮个数
public void onCreate(Bundle savedInstanceState){ //重写onCreat方法
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button=(Button)findViewById(R.id.Button01);
//获取屏幕中的按钮控件对象
Button.setOnClickListener( //为按钮添加OnClickListener接口实现 new View.OnClickListener(){
public void onClick(View v){
LinearLayout ll=( LinearLayout)findViewById(R.id.lla);
//获取线性布局对象
String msg=LinearActivity.this.getResources().gerString(R.string.button);
Button tempbutton=new Button(LinearActivity.this);
//创建一个Button对象
tempbutton.setText(msg+(++count)); //设置Button控件显示的内容
tempbutton.setWidth(80);//设置Button的宽度
ll.addView(tempbutton);//向线性布局中添加View
}
});
}
}
二、表格布局
TableLayout类简介
此布局为表格布局,适用于N行N列的布局格式。一个TableLayout由许多TableRow组成,一个TableRow就代表TableLayout中的一行。
TableRow是LinearLayout的子类,它的android:orientation属性值恒为horizontal,并且它的android:layout_width和android:layout_height属性值恒为MATCH_PARENT和WRAP_CONTENT。所以它的子元素都是横向排列,并且宽高一致的。这样的设计使得每个TableRow里的子元素都相当于表格中的单元格一样。在TableRow中,单元格可以为空,但是不能跨列。
在表格布局中,一个列的宽度由该列中最宽的那个单元格指定,而表格的宽度是由父容器指定的。在TableLayout中,可以为列设置三种属性。
Shrinkable,如果一个列被标识为shrinkable,则该列的宽度可以进行收缩,以使表格能够适应其父容器的大小。
Stretchable,如果一个列被标识为stretchable,则该列的宽度可以进行拉伸, 以使填满表格中空闲的空间。
Collapsed,如果一个列被标识为collapsed,则该列将会被隐藏。
注意:一个列可以同时具有Shrinkable和Stretchable属性,在这种情况下,该列的宽度将任意拉伸或收缩以适应父容器。
下面示例演示了一个TableLayout的布局结构,其中第二行只有两个单元格,而其余行都是三个单元格。
TableLayout继承自LinearLayout类,除了继承来自父类的属性和方法,TableLayout类中还包含表格布局所特有的属性和方法。
所有列设置为Shrinkable或Stretchable
案例:
1. 在布局管理器中实现布局:
在eclipse中新建一个项目。首先打开项目文件夹下res/values目录下的string.xml,写入一下内容:
<?xml version=”1.0”encoding=”utf-8”?>
<resources>
<string name=”app_name”>TableExample</string>
<string name=”tv1”>我自己是一行……我自己是一行</string>
<!--该值用于独占一行的列-->
<string name=”tvShort”>我的内容少</string>
<!—该值用于内容较少的列-->