————获取屏幕高宽————
- // 获取状态栏高度
- Rect frames = new Rect();
- MainTwoActivity.this.getWindow().getDecorView().getWindowVisibleDisplayFrame(frames);
- int statusBarHeights = frames.top;
- //获取标题栏高度
- int titleHeights = frames.height() - View.getheight();
- //获得屏幕高宽
- int heights = MainTwoActivity.this.getWindowManager().getDefaultDisplay().getHeight();
- int widths = MainTwoActivity.this.getWindowManager().getDefaultDisplay().getWidth();
- //正确获取手机屏幕的分辨率和密度
- DisplayMetrics dm = new DisplayMetrics();
- getWindowManager().getDefaultDisplay().getMetrics(dm);
- int widthPixels= dm.widthPixels;
- int heightPixels= dm.heightPixels;
- float density = dm.density;
- int screenWidth = widthPixels * density ;
- int screenHeight = heightPixels * density ;
在480*800的手机上的到的widthPixels值是320,heightPixels值是533,density的值是1.5。
注意: 此处DisplayMetrics不要使用context.getApplicationContext().getResources().getDisplayMetrics()来获取。
————关于inflate的使用————
- View view=View.inflate(this,R.layout.dialog_layout,null);
- TextViewdialog TV=(TextView)view.findViewById(R.id.dialog_tv);
————后台动态生成控件————
- //第一是容器,创建一个容器(这里用LinearLayout举例);
- LinearLayout lLayout = new LinearLayout(MainActivity.this);//(这里的this是一个Context变量);
- //第二是容器的细节,也就是属性部分;比如,容器里的控件是怎样排的;
- lLayout.setOrientation(LinearLayout.VERTICAL);
- //第三是容器里的控件,创建自己想要的控件;比如,
- EditText editText = new EditText(MainActivity.this);
- Button button = new Button(MainActivity.this);//这里的this都是一个Context变量;
- //第四是容器的细节,就是设置容器的属性;
- editText.setLines(3);
- editText.setGravity(Gravity.TOP|Gravity.LEFT);
- button.setText("插入随机表情");
- //第五是将控件加入到容器当中去;
- LinearLayout.LayoutParams lParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
- lParams.setMargins(0,0,0,0);//设置margin属性
- RelativeLayout.LayoutParams rParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
- rParams.addRule(RelativeLayout.CENTER_HORIZONTAL);//设置RelativeLayout控件下android:layout_centerHorizontal 属性
- rParams.addRule(RelativeLayout.BELOW, tvtime.getId()); //设置RelativeLayout控件下android:layout_below 属性
- rParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); //设置RelativeLayout控件下android:layout_alignParentLeft 属性
- rParams1.addRule(RelativeLayout.LEFT_OF,tv.getId()); //设置RelativeLayout控件下android:layout_toRightOf 属性
- lLayout.addView(editText, lParams);
- lLayout.addView(button, lParams);
- //第六是将容器将加入ContentView中;
- setContentView(lLayout);
————判断是否连接网络————
- public static boolean isNetworkAvailable(Context context) {
- ConnectivityManager connectivity = (ConnectivityManager) context
- .getSystemService(Context.CONNECTIVITY_SERVICE);
- if (connectivity != null) {
- NetworkInfo[] info = connectivity.getAllNetworkInfo();
- if (info != null) {
- for (int i = 0; i < info.length; i++) {
- if (info[i].getState() == NetworkInfo.State.CONNECTED) {
- return true;
- }
- }
- }
- }
- return false;
- }
加上相应的权限:
- <</span>uses-permission android:name="android.permission.INTERNET" />
- <</span>uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
————判断只可以输入数字————
1、
Android:digits属性:设置允许输入哪些字符。
假如只可以输入0到9的数字字符,设置为:android:digits ="1234567890"
2、
Integer.parseInt() 抛出的Exception来判断:
- try
- {
- Integer.parseInt(string);
- }
- catch
- {
- //发生了异常,那么就不是数字了。
- }
3、用正则表达式
- if(str.matches("^[0-9]")){
- //不是数字
- }else{
- //是数字
- }
常用的正则表达式:
^[1-9]\d{3}$ 非0开头的4位整数
^([1-9]\d{0,3})$ 非零的最大4位数字,可以是1位,2位,3位
[^a-zA-Z0-9] 只允许字母和数字
[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……& amp;*()——+|{}【】‘;:”“’。,、?] 清除掉所有特殊字符
————获取屏幕宽度和高度————
- WindowManager wm = (WindowManager) getContext()
- .getSystemService(Context.WINDOW_SERVICE);
- int width = wm.getDefaultDisplay().getWidth();
- int height = wm.getDefaultDisplay().getHeight();
- WindowManager wm = this.getWindowManager();
- int width = wm.getDefaultDisplay().getWidth();
- int height = wm.getDefaultDisplay().getHeight();
————Android 属性android:visibilty————
此属性意思是此视图是否显示,例如RelativeLayout中android:visibility="gone"
其有三个属性:visible显示;invisible不显示但是占用空间;gone不显示
在类中,可以设置其显示与否,setVisibility(View.GONE);不显示
setVisibility(View.VISIBLE);显示 如果想要得倒控件的显示状态 可以
通过:getVisibility()==View.GONE 或者getVisibility()==View.VISIBLE来作出判断
————Android CheckBox事件setOnCheckedChangeListener————
- checkbox.setOnCheckedChangeListener(new Compounon.OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(Compounon buttonView, boolean isChecked) {
- if(isChecked){
- System.out.println("勾选了...");
- }else{
- System.out.println("没勾选了...");
- }
- }});
————android中spinner级联设置默认选择setselection()函数无效————
对于两个spinner设计默认选择时,我使用的都是setselection(int position)函数,但这样的结果是只有第一级spinner能够实现默认选择的功能,第二级spinner死活都不行
后来尝试使用 setselection(int position,boolean animate)函数,就解决这个问题。后面的boolean 参数设为true.
————android中TextView内容过长加省略号————
在xml中:
- android:ellipsize = "end" //省略号在结尾
- android:ellipsize = "start" //省略号在开头
- android:ellipsize = "middle" //省略号在中间
- android:ellipsize = "marquee" //跑马灯
- android:singleline = "true" //最好加上一个约束
在后台代码中:
- tv.setEllipsize(TextUtils.TruncateAt.valueOf("END"));
- tv.setEllipsize(TextUtils.TruncateAt.valueOf("START"));
- tv.setEllipsize(TextUtils.TruncateAt.valueOf("MIDDLE"));
- tv.setEllipsize(TextUtils.TruncateAt.valueOf("MARQUEE"));
- tv.setSingleLine(true); //最好再加一个约束
不仅对于textview有此属性,对于editext也有,不过它不支持marquee
————android 开发环境的问题————
Download interrupted: Connection to https://dl-ssl.google.com refused
Failed to fectch URl https://dl-ssl.google.com/android/repository/addons_list.xml,
这个问题可能是系统问题,需要修改一下host文件:
方法如下:
用记事本打开C:\WINDOWS\system32\drivers\etc中的hosts文件
在最后一行添加
74.125.237.1 dl-ssl.google.com
成功解决问题。
————android 解决控件点击事件会穿透下面的界面————
给控件添加属性:android:clickable="true" 成功解决。
————Eclipse 添加主题样式 ————
方法1:你可以从Eclipse Marketplace中下载
Please visit here: http://eclipsecolorthemes.org/?view=plugin
下载下来后File->Import->General->Preferences->找到你的.epf文件->Finish
方法2:通过站点更新
eclipse:Help->Install New Software->Work with:Update Site - http://eclipse-color-theme.github.com/update
MyEclipse:Help->MyEclipse Confrontation Center->Software->add site->输入name:ColorTheme,URL:http://eclipse-color-theme.github.com/update->点击OK->在Personal Sites下的Eclipse Color Theme下的Eclipse Color Theme右键Add to Profile->等待五分钟安装插件的时间->点击右下角的apply changes
安装后需要重启Eclipse。重启后打开Window->Preferences->General->Apperance->Color Theme(未安装插件前是没有这个选项的)
————TextView 添加阴影效果————
- <</span>TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:shadowColor="#ff000000"
- android:shadowDx="2"
- android:shadowDy="2"
- android:shadowRadius="1"
- android:text="测试阴影"
- android:textSize="20sp" />