在线文档
1. 下面两个分别指定了控件的宽度和高度 android:layout_width: android:layout_height: 可选值有三种: match_parent fill_parent wrap_parent //前两者一样,官方推荐match_parent match_parent表示让当前控件的大小和父布局的大小一样 wrap_parent表示当前控件的大小能够刚好包含住里面的东西
2. TextView 位置:android:gravity="" //center,top,bottom,left,right,center_vertical(垂直方向居中),center_horizontal(水平方向居中) 颜色:android:textColor="#00ff00" 字体大小:android:textSize="20sp"
3.Button !!!注册一个监控器 ···1. 匿名方法 Button button=(Button)findViewById(R.id.button); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v); })
···2. 接口方法 Button button=(Button)findViewById(R.id.button); button.setOnClickListener(this);
public void onClick(View v){ switch(v.getId()){ case R.id.button: break; }}
4. EditText 提示性文字:android:hint=" " 最多行数:android:maxLines="2"
5. ImageView
图片在drawable文件下是ic_launcher.png
还可以通过代码动态地设置
ImageView imageView=(ImageView)fingViewById(R.id.image_view); imageView.setImageResource(R.drwaable.jelly_bean); //jelly_bean.png在res/drawable-hdpi
6. ProgressBar android:visibility="" 有三种值: visible,invisible,gone visible 表示控件可见,且默认可见 invisible 表示控件不可见,但是仍然占据原来的位置和大小,可以理解为透明 gone 表示控件不可见,且不占据原来的位置和大小
两种方法 ProgressBar progressBar=(ProgressBar)fingViewById(R.id.progress_bar); ···1 progressBar.getVisibility() 获得当前状态 ···2 progressBar.setVisibility(View.VISIBLE) 改变当前状态 // if (progressBar.getVisibility()==View.GONE) progressBar.setVisibility(View.VISIBILE)
7. AlertDialog //弹出一个对话框,这个对话框置顶于所有控件之上用于警告 AlertDialog.Builder dialog=new AlertDialog.Builder(MainActivity.this); dialog.setTitle("") dialog.setMessage("") //设置是否可取消设置 dialog.setCancelable(false)
//设置确定按钮的点击事件 dialog.setPositiveButton("OK", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog,int which) )
//设置取消按钮的点击事件 dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog,int which) )
dialog.show();
8. 四种基本布局
9. 线性布局(LinearLayout):会将他所包含的控件在线性方向上依次排列 android:orientation="vertical"垂直方向布局,还可以是horizontal方向布局 !!需注意指定horizontal后,内部控件绝对不能的话将宽度指定为match_parent,同理,vertical,内部空间高度不能指定为match_parent
属性: ···1 android:layout_gravity:指控件在布局里面的对齐方式,而android:gravity:指文字在控件里面的对齐方式,两个的可选值差不多 !!!当LinearLayout线性布局的排列方式为horizontal时,只有垂直方向上的布局方式才有效,like top,center,bottom,因为此时水平方向的长度是固定的,每添加一个控件,水平方向上的长度都会发生变化,同理,当LinearLayout线性布局的排列方式为vertical时,只有horizontal方向上的布局方式才有效
···2 android:layout_weight:允许使用比例来控制指定控件的大小 比如同一个horizontal线性布局下:
<EditText android:layout_width="0dp" android:layout_weight="1" />
<Button android:layout_width="0dp" android:layout_weight="1" /> 那么EditText占据屏幕宽度1/2的地方,由于我们使用了weight,那么宽度就不再由width决定
而 <EditText android:layout_width="0dp" android:layout_weight="1" />
<Button android:layout_width="wrap_content" /> //由Button按照wrap_content决定,其余的空间由edittext所占
10. RelativeLayout(相对布局):常用,可以通过相对布局来定位,因此属性非常多 ···1.相对于父布局定位 android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_alignParentBottom="true" android:layout_centerInParent="true" android:layout_alignParentLeft="true" android:layout_alignParentLeft="true" ···2.相对于控件定位 android:layout_above="@id/button3" //在控件Button3上面 android:layout_below="@id/button3" //在控件Button3下面 android:layout_toLeftOf="@id/button3" //在控件Button3左边 android:layout_toRightOf="@id/button3" //在控件Button3右边 !!!注意:button3必须先定义,否则找不到 ···3.也是相对于控件定位 android:layout_alignLeft+"@id/" //左边缘对齐 android:layout_alignRight+"@id/" //右边缘对齐 android:layout_alignTop+"@id/" //上边缘对齐 android:layout_alignBottom+"@id/" //下边缘对齐
11. FrameLayout 所有的布局都在左上方
12. TableLayout:允许使用表格的方式来排列控件
!!!尽量让每一行都拥有相同列数,不过有时也需要合并单元格
<TableLayout>
<TableRow> <EditText...../> <Button....../> </TableRow>
<TableRow> <EditText...../> <Button....../> </TableRow>
<TableRow> <Button android:layout_span="2"/> //让登陆按钮占据两行空间 </TableRow>
</TableLayout>
!!!为了充分利用屏幕的宽度,可以在TableLayout中 加入android:stretchColumns="1",假如表格未占满会拉伸第二列,同理,指定为0拉伸第一列
13. 所有的控件都继承或间接继承于View,所有的布局都继承于ViewGroup(继承于View)这个类,View是最基本的UI组件,可以在屏幕上绘制一块矩形,并响应这块区域的各种事件
14.自建布局(标题栏) ···1. layout文件夹下创建title.xml
···2. 设置背景,组件,尤其是 margin="5dip"//指定控件在上下左右四个方向的偏移距离当然,可以具体,android:layout_margin_Left="5dip"
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/background2"> <Button android:id="@+id/title_back" android:layout_margin="5dip" android:text="返回" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textColor="#000000" android:background="#e65b5b" android:gravity="center"/> <TextView android:id="@+id/title_text" android:layout_margin="5dip" android:text="通讯录" android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="0dip" android:textColor="#000000" android:gravity="center"/> <Button android:id="@+id/title_edit" android:layout_margin="5dip" android:text="编辑" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textColor="#000000" android:background="#e65b5b" android:gravity="center"/> </LinearLayout>···3. 在想用该布局中的地方加入就可以
15. 自建有效控件,意义:希望反映相同事件 ···1在java文件下创建一个类:
public class TitleLayout extends LinearLayout{ public TitleLayout( final Context context, AttributeSet attributeSet){ super(context,attributeSet); LayoutInflater.from(context).inflate(R.layout.title,this); } }
//通过LayoutInflater的from方法可以创建出一个LayoutInflater对象,然后调用inflater()方法就可以动态地加载出一个文件布局
···2.修改要加入该控件的活动的xml文件
!!!需要注意:需要指明文件的包名
<com.example.zzz.address.TitleLayout android:layout_width="match_parent" android:layout_height="match_parent" />···3. 注册按钮事件,再次修改代码
public class TitleLayout extends LinearLayout{ public TitleLayout( final Context context, AttributeSet attributeSet){ super(context,attributeSet); LayoutInflater.from(context).inflate(R.layout.title,this); Button button_back=(Button) findViewById(R.id.title_back); Button button_edit=(Button) findViewById(R.id.title_edit); button_back.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ((Activity)getContext()).finish(); } }); button_edit.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getContext(),"编辑",Toast.LENGTH_LONG).show(); Intent intent1 =new Intent ("com.example.zzz.address.LOGIN"); context.startActivity(intent1); } }); } }!!!注意新建布局和新建控件不要忘了有一样效果,只是布局没有功能
