android控件

来源:互联网 发布:学软件开发怎么样 编辑:程序博客网 时间:2024/06/04 17:52
一、通用属性
android:layout_weight="0|1"* 权重,在线性布局中,如果设置为1,则最后摆放,且占领剩余空间
* 首先按照控件声明的尺寸进行分配,然后再将剩下的尺寸按weight分配
* 可在父容器中设置 android:weightSum="2" 来规定子控件总共的weight数量
* 宽度 = 本身宽度 + 剩余宽度根据权重平分


二、位置属性
1)子控件在本控件的位置,没有layout
android:padding="" 内边距
android:gravity=""子控件在本控件的位置

2)本控件在父控件的位置,有layout
android:layout_margin="" 外边距

LinearLayout布局:
android:layout_gravity=""

RelativeLayout布局:
* 位置关系
android:layout_below="@id/xx"在某元素的下边
android:layout_above="@id/xx"在某元素的上边
android:layout_toLeftOf="@id/xx"在某元素的左边
android:layout_toRightOf="@id/xx"在某元素的右边

android:layout_centerHrizontal="t|f"水平居中(父控件中)
android:layout_centerVertical="t|f"垂直居中(父控件中)
android:layout_centerInparent="t|f"完全居中(父控件中)


* 对齐关系
android:layout_alignTop="@id/xx"本元素的上边缘和某元素的上边缘对齐
android:layout_alignLeft="@id/xx"本元素的左边缘和某元素的左边缘对齐
android:layout_alignBottom="@id/xx"本元素的下边缘和某元素的下边缘对齐
android:layout_alignRight="@id/xx"本元素的右边缘和某元素的右边缘对齐

android:layout_alignParentBottom="t|f"贴紧父元素的下边缘
android:layout_alignParentLeft="t|f"贴紧父元素的左边缘
android:layout_alignParentRight="t|f"贴紧父元素的右边缘
android:layout_alignParentTop="t|f"贴紧父元素的上边缘
android:layout_alignWithParentIfMissing="t|f"如果对应的兄弟元素找不到的话就以父元素做参照物

三、文本框,编辑框(TextView,EditText)
1)监听事件
editText.addTextChangedListener(new TextWatcher(){});//文本改变事件
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {});//焦点改变事件

2)属性
android:text=""设置显示文字
android:autoLink="" 设置超链接格式(一般用于TextView底部超链接)

android:editable="true"是否可以编辑
android:password="true"是否密码显示
android:lines="3"文本编辑框行数(显示行数)
android:maxLines="3"最大行数(数字行数)
android:maxLength="3"限制输入字数
android:hint="asdf"默认提示字
android:numeric="integer"integer(正整数)、signed(整数)、decimal(浮点)
android:phoneNumber="true"是否数字显示,弹数字键盘


android:digits="asdf"过滤字符串
android:selectAllOnFocus="true"获取焦点时是否选中自我
android:capitalize="cwj1987"这样仅允许接受输入cwj1987,一般用于密码验证

android:imeOptions="actionNext"输入法选项,移动到下一个输入框
android:imeOptions="actionDone"输入法选项,关闭键盘
android:singleLine="true"是否单行显示

3)密码明文密文显示
editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
editText.setTransformationMethod(PasswordTransformationMethod.getInstance());

四、按钮,图片按钮,图片(Button、ImageButton、ImageView)
1)监听事件
button.setOnClickListener(new View.OnClickListener() {});//点击事件
button.setOnTouchListener(new View.OnTouchListener() {//按钮等触摸事件
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()){
case MotionEvent.ACTION_DOWN:
break;
}
return true;
}
});

2)Button属性
android:background="@drawable/cal_edittext_bg"
android:drawableTop="@drawable/cal_edittext_bg"
android:drawablePadding="10dp"
android:onClick="onClick"

3)ImageButton属性
android:background="@drawable/cal_edittext_bg"
android:src="@drawable/cal_edittext_bg"
android:scaleType="fitXY"

4)ImageView属性
android:background="@drawable/cal_edittext_bg"
android:src="@drawable/cal_edittext_bg"
android:scaleType="fitXY"
* 默认:不缩放,左上角开始绘制图
* center:不缩放,图像放在ImageView中间(用于小图片按钮)
* centerCrop:等比缩放,图片完全覆盖ImageView
* centerInside:等比缩放,使图片完全显示
* fitXY:独立缩放,贴合ImageView

5)差异
* ImageButton不支持setText,而Button支持,ImageButton支持setImageURI,而Button不支持
* ImageButton有Button的状态,但是ImageView没有
* ImageButton拥有默认背景android:background="@android:drawable/btn_default"
* ImageButton支持9.png图片,ImageView不支持
* 点9图片需要放在drawable-hdpi里面

5.单选按钮,复选按钮,开关按钮(RadioGroup,RadioButton,CheckBox,ToggleButton)
1)监听事件
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {});
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {});
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {});

2)属性
RadioGroup中的选中android:checkedButton="@+id/at_rb_rb1"
CheckBox中的选中android:checked="true"
ToggleButton就相当于CheckBox

6.日期控件,时间控件(DatePicker,TimePicker)
1)监听事件
datePicker.init(year, month, day, new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker datePicker, int year, int month, int day) {}
});
timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {});

2)属性
android:startYear="2005"
android:endYear="2016"

3)日历类
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);


7.滚动页面(ScrollView,HorizontalScrollView)
1)属性
android:scrollbars="none"// 设置滚动条

android:orientation="vertical"
android:fillViewport="true"
注:ScrollView里面只能存在一个组件,而且是垂直摆放的


8.进度条,拖动条,评分条(ProgressBar,SeekBar,RatingBar)
1)监听事件
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {});
ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {});

2)ProgressBar属性
style="@android:style/Widget.ProgressBar.Inverse"转圈圈
style="@android:style/Widget.ProgressBar.Horizontal"水平样式
android:max="100"最大值
android:progress="30"进度

3)SeekBar属性
android:max="100"最大值
android:progress="30"进度
android:thumb="@mipmap/ic_launcher"控制图片

4)RatingBar属性
android:numStars="5"星星总数量
android:rating="3.5"默认数量
android:stepSize="0.5"最小步伐
style="?android:attr/ratingBarStyleSmall"星星的样式

5)线程是滚动条滚动
// 关注点:线程里面不能直接处理控件
new Thread(new Runnable() {
public void run() {
for(int i = 0;i<=100;i++){
progressInt = i;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.sendEmptyMessage(0);
}
}
}).start();

// 用Handler处理接受的值
private Handler handler = new Handler(){
public void handleMessage(Message msg) {
switch (msg.what){
case 0:
progressBar.setProgress(progressInt);
break;
}
super.handleMessage(msg);
}
};

9.网页控件(WebView)
1)WebView设置
WebView webView = (WebView) findViewById(R.id.tabspec1_wv_wv1);
WebSettings webSettings = webView.getSettings();    // 获取设置
webSettings.setJavaScriptEnabled(true);             // 设置能执行js脚本
webSettings.setAllowFileAccess(true);               // 设置可以访问文件
webSettings.setBuiltInZoomControls(true);           // 设置支持缩放
webView.loadUrl("file:///android_asset/welcome.html");// 加载页面(建asset文件夹,与res同级)

//网页可在webView里面覆盖加载
webView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
2)返回键设置
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView .canGoBack()) { 
webView.goBack();
return true;
}
return false;
}

3)网络权限
<uses-permission android:name="android.permission.INTERNET" />


/**
 * ==============================以下应用适配器======================================
 */
10.画廊、图片选择(Gallery和ImageSwitcher)
1)监听事件
gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {});// gallery选择事件,一般用于画布设图片
gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {});// gallery点击事件

2)Gallery属性
android:layout_width="match_parent"
android:unselectedAlpha="0.6"
android:spacing="4dp"


3)ImageSwitcher配置
imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
public View makeView() {
ImageView imageView = new ImageView(TestActy.this);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT
));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setBackgroundColor(0xff0000);
return imageView;
}
});
// 设置图片
imageSwitcher.setImageResource(imgInt[i]);

11.自动提示框,下拉框(AutoCompleteTextView和Spinner)
1)监听事件
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {});
autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {});

2)AutoCompleteTextView属性
android:completionHint="123"提示标题
android:completionThreshold="2"至少几个字符才会提示
android:dropDownWidth="match_parent"下拉框宽度
android:dropDownHeight="wrap_content"下拉框高度

3)Spinner获值和设值
spinner.getSelectedItemPosition();
spinner.setSelection(i);


12.网格(GridView)
1)监听事件
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {});


2)属性
android:numColumns="4"列数
android:verticalSpacing="10dp"垂直间隔
android:horizontalSpacing="10dp"水平间隔

13.列表(ListView)
1)监听事件
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {});

2)属性
宽高需要 match_parent
android:listSelector="@android:color/transparent"选中的背景色
android:divider="@null"无分割线
android:scrollbars="none"无滚动条
android:fadingEdge="none"渐变区域的宽度
android:scrollingCache="false"滚动缓存

3)ListView消息更新
1.更新适配器里的数据源
2.刷新适配器
myAdapter.notifyDataSetChanged();
3.ListView设置动态效果
listView.smoothScrollToPosition(0);


14.可展开的列表(ExpandableListView)
1)监听事件
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {});

2)适配器配置
1.继承BaseExpandableListAdapter
2.用二维数组或者Item嵌套Item传递数据源

3)设置ExpandableListView默认显示图标为null
expandableListView.setGroupIndicator(null);

15.页面轮换器(ViewPager)
1)监听事件
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {});

2)适配器配置
1.继承PagerAdapter
2.重写方法
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(imageViewList.get(position));
}
public Object instantiateItem(ViewGroup container, int position) {
container.addView(imageViewList.get(position),0);
return imageViewList.get(position);
}
public int getCount() {
return imageViewList.size();
}
public boolean isViewFromObject(View view, Object object) {
return view == object;
}

3)自定义选项卡设置页面
设置第几个选项:viewPager.setCurrentItem(0);

16.AlertDialog
1)普通AlertDialog
customDialog.setCanceledOnTouchOutside(false);// dialog
(1)AlertDialog的创建
if(alertDialog == null){
AlertDialog.Builder builder = new AlertDialog.Builder(context);// 建造者模式,用Builder内部类去建造
builder.setTitle("提示框");
builder.setIcon(R.mipmap.ic_launcher);
builder.setMessage("确定退出吗");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {});
alertDialog = builder.create();

}
alertDialog.show();

(2)AlertDialog设置返回按钮,保证页面安全
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {//重写父类onKeyDown方法
switch (keyCode){
case KeyEvent.KEYCODE_BACK:
if(alertDialog != null && alertDialog.isShowing()){
alertDialog.dismiss();
return true;
}
showAlertDialog();
break;
}
return true;
}

2)单选(SingleChoice)
setSingleChoiceItems(new String[]{"篮球","足球","排球"}, 0, new DialogInterface.OnClickListener() {})

3)复选(MultiChoice)
setMultiChoiceItems(new String[]{"篮球","足球","排球"}, new boolean[]{false,false,false}, new DialogInterface.OnMultiChoiceClickListener() {})

4)类单选(Items)
builder.setItems(arr, new DialogInterface.OnClickListener() {});
// 和单选AlertDialog的区别是没有右边的单选按钮,且会有dismiss()效果

5)时间日期对话框
new DatePickerDialog(Context.this, new DatePickerDialog.OnDateSetListener() {},year,month,day_of_month).show();
new TimePickerDialog(Context.this, new TimePickerDialog.OnTimeSetListener() {},hour_of_day,minute,true).show();


6)进度条对话框
progressDialog1 = new ProgressDialog(mContext);
progressDialog1.setTitle("大片");
progressDialog1.setMessage("下载中。。。");
progressDialog1.setCancelable(false);// 响应系统返回键的语句
progressDialog1.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); // 圈圈格式ProgressDialog.STYLE_SPINNER
progressDialog1.setMax(100);
progressDialog1.show();

7)绑定自定义布局或控件
builder.setView(view);

8)自定义Dialog
1)自定义Dialog,继承Dialog
public MyCustomDialog(Context context) {
super(context,R.style.MyDialog);
setContentView(R.layout.dialog_listviewdialog);
titleTV = (TextView) findViewById(R.id.listviewdialog_tv_title);
listViewLV = (ListView) findViewById(R.id.listviewdialog_lv_main);
queDingBN = (Button) findViewById(R.id.listviewdialog_bn_queding);
}
2)实现style样式
style样式
<style name="MyDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
</style>


9)自定义PopupWindow
popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setFocusable(true);// 可获取焦点
popupWindow.setBackgroundDrawable(new BitmapDrawable());// 响应系统返回键的语句
//方法一:
popupWindow.showAsDropDown(findViewById(R.id.main_bn_anxia));
//方法一:
popupWindow.showAtLocation(findViewById(R.id.main_bn_anxia), Gravity.BOTTOM,0,0);


17.TabHost选项卡(弃用):
1)布局
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/tabhost"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
></FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"
></TabWidget>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="60dp"
android:orientation="horizontal"
android:gravity="center_vertical"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@mipmap/tabhost_1"
/>
</LinearLayout>
</LinearLayout>
</TabHost>
2)代码
tabHost = getTabHost();//需要继承TabActivity
//添加进tabHost
tabHost.addTab(tabHost.newTabSpec("11").setIndicator("选项卡1").setContent(new Intent(TabHostActy.this, MyQQActivity.class)));
tabHost.addTab(tabHost.newTabSpec("22").setIndicator("选项卡2").setContent(new Intent(TabHostActy.this, TabSpec2Acty.class)));
//设置ImageView监听事件
public void onClick(View view) {
imageView1.setImageResource(R.mipmap.tabhost_1);


switch (view.getId()) {
case R.id.tabhost_tv_tv1:
imageView1.setImageResource(R.mipmap.tabhost_1_);
tabHost.setCurrentTab(0);
break;
case R.id.tabhost_tv_tv2:
imageView2.setImageResource(R.mipmap.tabhost_2_);
tabHost.setCurrentTab(1);
break;
}


}

18.SlidingDrawer拖动框:(弃用)
<SlidingDrawer
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_alignParentBottom="true"
android:handle="@+id/acty_test_iv_ic"
android:content="@+id/acty_test_ll_ll"
>
<ImageView
android:id="@+id/acty_test_iv_ic"
android:layout_width="25dp"
android:layout_height="17dp"
android:src="@mipmap/a4z"
android:scaleType="fitXY"
/>
<LinearLayout
android:id="@+id/acty_test_ll_ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#44000000">
</LinearLayout>
</SlidingDrawer>










0 0
原创粉丝点击