控件类概述
View
可视化控件的基类
属性名称 | 对应方法 | 描述 |
android:background | setBackgroundResource(int) | 设置背景 |
android:clickable | setClickable(boolean) | 设置View是否响应单击事件 |
android:visibility | setVisibility(int) | 控制View的可见性 |
android:focusable | setFocusable(boolean) | 控制View是否可以获取焦点 |
android:id | setId(int) | 为View设置标识符,可通过findViewById方法获取 |
android:longClickable | setLongClickable(boolean) | 设置View是否响应长单击事件 |
android:soundEffectsEnabled | setSoundEffectsEnabled(boolean) | 设置当View触发单击等事件时是否播放音效 |
android:saveEnabled | setSaveEnabled(boolean) | 如果未作设置,当View被冻结时将不会保存其状态 |
android:nextFocusDown | setNextFocusDownId(int) | 定义当向下搜索时应该获取焦点的View,如果该View不存在或不可见,则会抛出RuntimeException异常 |
android:nextFocusLeft | setNextFocusLeftId(int) | 定义当向左搜索时应该获取焦点的View |
android:nextFocusRight | setNextFocusRightId(int) | 定义当向右搜索时应该获取焦点的View |
android:nextFocusUp | setNextFocusUpId(int) | 定义当向上搜索时应该获取焦点的View |
ViewGroup
ViewGroup的子控件既可以是普通的View,也可以是ViewGroup。一些高级控件如Gallery、GridView等都是继承自ViewGroup。Android中为每种不同的布局提供一个ViewGroup的子类,如LinearLayout、TableLayout、RelativeLayout、FrameLayout、AbsoluteLayout等。
线性布局android.widget.LinearLayout
http://android.toolib.net/reference/android/widget/LinearLayout.html
http://android.toolib.net/reference/android/widget/LinearLayout.LayoutParams.html
LinearLayout是最简单的布局之一,它提供了控件水平或垂直排列的模型,可以通过设置控件的weight参数控制各个控件在容器中的相对大小。LinearLayout布局的属性也是既可以通过布局XML文件设置,也可以通过成员方法进行设置。
LinearLayout常用的属性及对应设置方法
属性名称 | 对应方法 | 描述 |
android:orientation | setOrientation(int) | 设置线性布局的朝向,可取horizontal和vertical两种排列方式 |
android:gravity | setGravity(int) | 设置线性布局的内部元素的布局方式 |
gravity可取的属性及说明
属性值 | 说明 |
top | 不改变控件大小,对齐到容器顶部 |
bottom | 不改变控件大小,对齐到容器底部 |
left | 不改变控件大小,对齐到容器左侧 |
right | 不改变控件大小,对齐到容器右侧 |
center_vertical | 不改变控件大小,对齐到容器纵向中央位置 |
center_horizontal | 不改变控件大小,对齐到容器横向中央位置 |
center | 不改变控件大小,对齐到容器中央位置 |
fill_vertical | 若有可能,纵向拉伸以填满容器 |
fill_horizontal | 若有可能,横向拉伸以填满容器 |
fill | 若有可能,纵向横向同时拉伸填满容器 |
相对布局android.widget.RelativeLayout
http://android.toolib.net/reference/android/widget/RelativeLayout.html
http://android.toolib.net/reference/android/widget/RelativeLayout.LayoutParams.html
XML布局配置
<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"
android:gravity="left"
android:id="@+id/lls"
tools:context=".MainActivity" >
<Button
android:id="@+id/button01"
android:text="@string/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
/>
</RelativeLayout>
Activity
package com.example.android_layout;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
/**
* Button基本使用方法
* 1、添加Button控件到XMl布局文件中
* 2、给按钮一个id号,这是按钮唯一的名字
* 3、通过程序获取按钮
* 4、处理按钮点击
* ①第一种是通过onClick属性,通过这个属性设置处理点击事件的方法名,在Activity中实现这个方法。
* ②另一种方法是典型的事件监听机制的应用形式,使用setOnClickListener添加监听器对象
* @author liuxinming
*
*/
public class MainActivity extends Activity {
int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取屏幕控件
Button button = (Button) findViewById(R.id.button01);
//添加
button.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//获取布局管理器
RelativeLayout ll = (RelativeLayout) findViewById(R.id.lls);
//动态创建button 对象
String msg = MainActivity.this.getString(R.string.app_name);
Button tempbutton = new Button(MainActivity.this);
tempbutton.setText(msg+(++count));
tempbutton.setId(count);
/**
* LayoutParams继承于Android.View.ViewGroup.LayoutParams.
* LayoutParams相当于一个Layout的信息包,它封装了Layout的位置、高、宽等信息。假设在屏幕上一块区域是由一个Layout占领的,如果将一个View添加到一个Layout中,最好告诉Layout用户期望的布局方式,也就是将一个认可的layoutParams传递进去。
可以这样去形容LayoutParams,在象棋的棋盘上,每个棋子都占据一个位置,也就是每个棋子都有一个位置的信息,如这个棋子在4行4列,这里的“4行4列”就是棋子的LayoutParams。
*********
*RelativeLayout下动态设置子控件居中
*********
*/
RelativeLayout.LayoutParams buttonLayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
buttonLayoutParams.addRule(RelativeLayout.BELOW,tempbutton.getId());
buttonLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
buttonLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
tempbutton.setLayoutParams(buttonLayoutParams);
ll.addView(tempbutton,buttonLayoutParams);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
上面案例是布局一个按钮,然后通过Activity设置一个监听按钮事件,点击后添加一个新按钮出来