Android菜鸟
常用的基本控件有
属性名称 |
作用描述 |
TextView |
显示文本信息 |
Button |
普通按钮 |
EditText |
可编辑的文本框组件(输入框) |
ImageView |
用于显示图片 |
ImageButton |
图片按钮 |
CheckBox |
复选框 |
RadioGroup |
单选按钮组 |
Spinner |
下拉列表组件 |
ProgressBar |
进度条 |
SeekBar |
拖动条 |
RatingBar |
评分组件 |
ListView |
列表 |
Diaolog |
对话框 |
Toast |
信息提示组件 |
TextView在xml中的基本属性有
属性名称 |
作用描述 |
android:autoLink |
设置是否当文本为 URL 链接/email/电话号码/map 时,文本显示为可点击的链接。可选值 (none/web/email/phone/map/all) |
android:digits |
设置允许输入哪些字符。如“1234567890.+-*/%\n()” |
android:drawableBottom |
在 text 的下方输出一个 drawable,如图片。如果指定一个颜色的话会把 text 的背景设为该颜 色,并且同时和 background 使用时覆盖后者。 |
android:drawablePadding |
设置 text 与 drawable(图片)的间隔,与 drawableLeft、drawableRight、drawableTop、 drawableBottom 一起使用,可设置为负数,单独使用没有效果。 |
android:gravity |
设置文本位置,如设置成“center”,文本将居中显示。 |
android:hint |
Text 为空时显示的文字提示信息,可通过 textColorHint 设置提示信息的颜色。比较奇怪的是 TextView 本来就相当于 Label,怎么会不设置 Text?! |
android:marqueeRepeatLimit |
在 ellipsize 指定 marquee 的情况下,设置重复滚动的次数,当设置为 marquee_forever 时表 示无限次。 |
android:lines |
设置文本的行数,设置两行就显示两行,即使第二行没有数据。 |
android:phoneNumber |
设置为电话号码的输入方式。 |
android:singleLine |
设置单行显示。如果和 layout_width 一起使用,当文本不能全部显示时,后面用“…”来表示。 如 android:text="test_ singleLine " android:singleLine="true" android:layout_width="20dp"将 只显示“t…”。如果不设置 singleLine 或者设置为 false,文本将自动换行 |
android:text |
设置显示文本. |
android:textSize |
设置文字大小,推荐度量单位”sp”,如”15sp” android:textStyle 设置字形[bold(粗体) 0, italic(斜体) 1, bolditalic(又粗又斜) 2] 可以设置一个或多个,用“|”隔开 |
android:typeface |
设置文本字体,必须是以下常量值之一:normal 0, sans 1, serif 2, monospace(等宽字体) |
android:height |
设置文本区域的高度,支持度量单位:px(像素)/dp/sp/in/mm(毫米) |
android:width |
设置文本区域的宽度,支持度量单位:px(像素)/dp/sp/in/mm(毫米)。 |
而TextView在类中的使用
方法 |
功能描述 |
返回值 |
TextView |
TextView的构造方法 |
Null |
getDefaultMovementmethod |
获取默认的箭头按键移动方式 |
Movementmethod |
getText |
获得TextView 对象的文本 |
CharSquence |
length |
获得 TextView 中的文本长度 |
Int |
getEditableText |
取得文本的可编辑对象,通过 这个对象可对 TextView 的文本进行操作,如 在光标之后插入字符 |
Void |
setCompoundDrawables |
设置图像显示的位置,在 设置该 Drawable 资源之前需要调用 setBounds (Rect) |
Void |
setCompoundDrawablesWithIn trinsicBounds |
设置 Drawable 图像的显示位置,但其边界不变 |
Void |
setTextColor |
设置文本显示的颜色 |
Void |
setHighlightColor |
设置文本选中时显示的颜色 |
Void |
setShadowLayer |
设置文本显示的阴影颜色 |
Void |
setHintTextColor |
设置提示文字的颜色 |
Void |
举例
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please choose you like !"
android:textSize="40dp"
android:textColor="@android:color/black"
android:id="@+id/yf_1"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"/>
</TextView>
EditText在xml中的基本属性
属性名称 |
作用描述 |
android:inputType |
设置文本类型 |
android:digits |
设置输入哪些符号 |
android:hint |
设置编辑框为空时显示的提示信息 |
android:password |
设置只能输入密码,以“.”显示文本 |
android:singleLine |
设置文本单行显示 |
android:editable |
设置是否可编辑。仍然可以获取光标,但是无法输入 |
requestFocus() |
使当前组件对象获得焦点 |
android:ems |
设置 TextView 的宽度为 N 个字符的宽度 |
而EditText在类中的使用
方法 |
功能描述 |
返回值 |
getDefaultEditable |
获取是否默认可编辑 |
boolean |
setEllipse |
设置文件过长时控件的显示方式 |
void |
setFreeezesText |
设置保存文本内容及光标位置 |
void |
getFreeezesText |
获取保存文本内容及光标位置 |
boolean |
setGravity |
设置文本框在布局中的位置 |
void |
getGravity |
获取文本框在布局中的位置 |
int |
setHint |
设置文本框为空时,文本框默认显示的 字符 |
void |
getHint |
获取文本框为空时,文本框默认显示的 字符 |
Charsequence |
setIncludeFontPadding |
设置文本框是否包含底部和顶端的额外空白 |
void |
setMarqueeRepeatLimit |
在 ellipsize 指定 marquee 的情况下,设置重复滚动的次数, 当设置为 marquee_forever 时表示无限次 |
void |
举例
<EditText
android:id="@+id/yf_1"
android:inputType="date"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxLength="40"
android:hint=" 输入电话号码 "
android:textColorHint="@android:color/black"
android:phoneNumber="true"
android:imeOptions="actionGo">
</EditText>
属性名称 |
功能描述 |
android:layout_height |
设置控件高度,可选值:fill_parent,warp_content,px |
android:layout_width |
设置控件宽度,可选值:fill_parent,warp_content,px |
android:text |
设置控件名称,可以是任意字符 |
android:layout_gravity |
设置控件在布局中的位置,可选项: top,left,bottom,right,center_vertical,fill_vertica,fill_horizonal,center,fill 等 |
android:layout_weight |
设置控件在布局中的比重,可选值:任意的数字 |
android:textColor |
设置文字的颜色 |
android:bufferType |
设置取得的文本类别,normal、spannable、editable |
android:hint |
设置文本为空是所显示的字符 |
android:textColorHighlight |
设置文本被选中时,高亮显示的颜色 |
android:inputType |
设置文本的类型,none,text,textWords 等 |
方法 |
功能描述 |
返回值 |
Button |
Button 类的构造方法 |
Null |
onKeyDown |
当用户按键时,该方法调用 |
Boolean |
onKeyUp |
当用户按键弹起后,该方法被调用 |
Boolean |
onKeyLongPress |
当用户保持按键时,该方法被调用 |
Boolean |
onKeyMultiple |
当用户多次调用时,该方法被调用 | Boolean
invalidateDrawable |
刷新 Drawable 对象 |
void |
scheduleDrawable |
定义动画方案的下一帧 |
void |
unscheduleDrawable |
取消 scheduleDrawable 定义的动画方案 |
void |
onPreDraw |
设置视图显示,列如在视图显示之前调整滚动轴的边界 |
Boolean |
sendAccessibilityEvent |
发送事件类型指定的 AccessibilityEvent。发送请求之前,需要检查 Accessibility 是否打开 void sendAccessibilityEventUnchecked 发送事件类型指定的 AccessibilityEvent。发送请求之前,不需要检查 Accessibility 是否打开 |
void |
setOnKeyListener |
设置按键监听 void |
举例
<Button
android:orientation="vertical"
android:layout_height="wrap_content"
android:id="@+id/yf_2"
android:layout_width="wrap_content"
android:textColor="@android:color/black""
android:text=" 这是 按钮 , 可点击 " />
而其它控件在xml中的使用方法都大同小异,接下来说的就是布局
目前所学的线性布局
LinearLayout 的使用
<!-- 水平线性布局 -->
<LinearLayout
android:layout_height="100dip"
android:orientation="horizontal"
android:layout_width="fill_parent">
<TextView
android:background="@android:color/black"
android:layout_width="wrap_content"
android:gravity="center_horizontal"
android:text=" 第一列 "
android:layout_weight="1"
android:layout_height="50dip">
</TextView>
<TextView
android:background="@android:color/black"
android:layout_width="wrap_content"
android:gravity="center_horizontal"
android:text=" 第二列 "
android:layout_weight="1"
android:layout_height="50dip">
</TextView>
<TextView
android:background="@android:color/black"
android:layout_width="wrap_content"
android:gravity="center_horizontal"
android:text=" 第三列 "
android:layout_weight="1"
android:layout_height="50dip">
</TextView>
</LinearLayout>
<!-- 垂直线性布局 -->
<LinearLayout
android:layout_height="100dip"
android:orientation="vertical"
android:layout_width="fill_parent">
<TextView
android:background="@android:color/black"
android:layout_width="fill_parent"
android:gravity="center_horizontal"
android:text=" 第一行 "
android:layout_weight="1"
android:layout_height="50dip">
</TextView>
<TextView
android:background="@android:color/black"
android:layout_width="fill_parent"
android:gravity="center_horizontal"
android:text=" 第二行 "
android:layout_weight="1"
android:layout_height="50dip">
</TextView>
</LinearLayout>
Dialog对话框
Android 中实现对话框可以使用 AlterDialog.Builder 类,还可以自定义对话框。如果对话框设置了按钮,那么就需要对其设置监听 OnClickListener。
Dialog的使用
首先在 Dialog.xml 中定义一个对话框
然后在 Activity 中使用 AlterDialog 创建对话框
发生的错误
1.布局有时候会因为一时大意而失去了头或尾,所以在写布局时要避免一些因为大意而导致的错误
2.有时候因为xml中对于某个控件没有正确命名而导致类发生了关联的错误
其中有关控件更多的属性和功能可以看以下的文件
http://pan.baidu.com/s/1kVt5iGf
(出处为百度)
用我家李易峰来给大家卖个萌
