今天讲的内容主要是和布局有关。布局主要描述元素、结构和相互之间的关系。Android中常用的布局包括RelativeLayout、LinearLayout、TableLayout、FrameLayout以及AbsoluteLayout。其中AbsoluteLayout由于其局限性已被淘汰。目前比较流行使用的是LinearLayout和TableLayout。
先从RelativeLayout说起。Android程序的默认布局就是RelativeLayout。使用相对布局的好处是拖动控件时比较方便,但是页面的美观性不好控制。
特点:能够最大程度的使各种控件在界面显示。
RelativeLayout可以通过已存在的视图确定其它视图的位置。这样可以避免使用多重布局,提高了效率。
例如:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/main_rl_whitetiger"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:adjustViewBounds="true"
android:maxHeight="200px"
android:maxWidth="200px"
android:src="@drawable/whitetiger" />
<ImageView
android:id="@+id/main_rl_turtle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/main_rl_whitetiger"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:adjustViewBounds="true"
android:maxHeight="200px"
android:maxWidth="200px"
android:src="@drawable/turtle" />
<ImageView
android:id="@+id/main_rl_phenix"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/main_rl_whitetiger"
android:layout_alignLeft="@+id/main_rl_turtle"
android:layout_marginBottom="19dp"
android:adjustViewBounds="true"
android:maxHeight="200px"
android:maxWidth="200px"
android:src="@drawable/phenix" />
<ImageView
android:id="@+id/main_rl_dragon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/main_rl_whitetiger"
android:layout_alignParentLeft="true"
android:layout_marginLeft="16dp"
android:adjustViewBounds="true"
android:maxHeight="200px"
android:maxWidth="200px"
android:src="@drawable/dragon" />
</RelativeLayout>
运行效果:
属性 | 描述 |
android:layout_centerInParent | 在父视图中正中心 |
android:layout_centerHorizontal | 在父视图的水平中心线 |
android:layout_centerVertical | 在父视图的垂直中心线 |
android:layout_alignParentLeft | 紧贴父视图左部 |
android:layout_alignParentBottom | 紧贴父视图底部 |
android:layout_alignParentTop | 紧贴父视图顶部 |
android:layout_ParentRight | 紧贴父视图右部 |
android:layout_alignTop | 与指定视图顶部对齐 |
android:layout_alignBottom | 与指定视图底部对齐 |
android:layout_alignLeft | 与指定视图左部对齐 |
android:layout_alignRight | 与指定视图右部对齐 |
android:layout_above | 在指定视图上方 |
android:layout_below | 在指定视图下方 |
android:layout_toLeft | 在指定视图左方 |
android:layout_toRight | 在指定视图右方 |
线性布局(LinearLayout):
线性布局中有一个重要的属性android:orientation。它具有两个重要的属性。
(1)水平 Horizontal
(2)垂直 Vertical
通常会作为某个控件的局部格式。除此之外,线性布局是应用最多的。
表格布局(TableLayout):
(1)每一个控件作为表格的一列来使用
行规正的数据显示,没有跨行跨列,即合并行合并列。要实现合并行、合并列需要嵌套其它的布局。
可在java代码中创建布局,可以通过代码的方式构建布局。弊端:程序如果不运行看不到效果,在实际开发的过程中基本不采用。用户可以使用任何一种或二者结合。
Android中控件的创建方式有两种:
(1)在xml中描述,提供相应的属性值,在java类中通过findViewById方法找到该控件。
(2)在java中通过new的方式直接构建。例如:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout=(RelativeLayout) findViewById(R.id.main_layout);
LinearLayout linear=new LinearLayout(this);
layout.addView(linear);
}
这种方式是极个别的情况下构建的。比如,手机弹出对话框的时候,窗口的各种元素的排列是通过这种方式排列的
private void findViewById()
{
this.btnEdit.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
final EditText editname=new EditText(MainActivity.this);
editname.setText("请输入姓名:");
AlertDialog.Builder alert=new AlertDialog.Builder(MainActivity.this);
alert.setTitle("提示");
alert.setView(editname);
alert.setPositiveButton("修改",new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
//内部类中只能使用final修饰的局部变量
String str=editname.getText().toString();
textmsg.setText(str);
}
});
alert.setNegativeButton("取消", null);
alert.show();
}
});
}
内部类中不可用局部变量。
附:
常用对话框:
(1)显示消息
(2)显示信息列表,并显示选择的内容
(3)显示带有单选按钮的对话框
(4)显示带有输入框的对话框
先创建文本框,再通过对话框的SetView方法把文本框添加到对话框。
(5)使用复杂布局的对话框
掌握对话框的基本操作
附一个对话框的代码:
private void setListener()
{
this.textType.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
AlertDialog.Builder alert=
new AlertDialog.Builder(MainActivity.this);
alert.setTitle("选择类型");
alert.setItems(types, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
textType.setText(types[which]);
}
});
alert.setNegativeButton("取消", null);
alert.show();
}
});
}
(3)框架布局(FrameLayout):
又称为“层叠布局”。
编程的过程中一个控件出现在另一个控件上方使用FrameLayout。
是最简单的界面布局,用于存放一个元素的空包空间,子元素的位置不能指定,只能放在左上角。
例如设计抽奖程序的界面:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="@drawable/bg">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/main_image_rotate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/rotate"
android:layout_centerInParent="true"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/main_image_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bt"
android:layout_centerInParent="true"
android:maxWidth="150dp"
android:maxHeight="150dp"
android:adjustViewBounds="true"
android:paddingBottom="35dp"
/>
</RelativeLayout>
</FrameLayout>
</RelativeLayout>
执行效果