Android开发之获取屏幕分辨率
2015-12-10 11:03
417 查看
在实际开发中,常常会需要根据不同的屏幕分辨率来布局不同的UI组件。那么手机屏幕的分辨率又该如何获取?
实现代码:
//获取屏幕分辩率
XML:
实现代码:
02 |
03 | import android.app.Activity; |
04 | import android.os.Bundle; |
05 | import android.util.DisplayMetrics; |
06 | import android.widget.TextView; |
07 |
08 | public class DisplayDemoActivity extends Activity { |
09 | /** Called when the activity is first created. */ |
10 | @Override |
11 | public void onCreate(Bundle savedInstanceState) { |
12 | super .onCreate(savedInstanceState); |
13 | setContentView(R.layout.main); |
14 | DisplayMetrics dm = new DisplayMetrics(); |
15 | getWindowManager().getDefaultDisplay().getMetrics(dm); |
16 | int width = dm.widthPixels; |
17 | int height = dm.heightPixels; |
18 |
19 | TextView textView = (TextView)findViewById(R.id.textId); |
20 | textView.setText( "手机屏幕分辨率:" +width+ "*" +height); |
21 | } |
22 | } |
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
03 | android:layout_width = "fill_parent" |
04 | android:layout_height = "fill_parent" |
05 | android:orientation = "vertical" > |
06 |
07 | < TextView |
08 | android:layout_width = "fill_parent" |
09 | android:layout_height = "wrap_content" |
10 | android:text = "@string/hello" |
11 | android:id = "@+id/textId" |
12 | /> |
13 |
14 | </ LinearLayout > |
相关文章推荐
- 解决Android Studio启动失败
- Android中通知栏Notification详解以及自定义Notification
- android怎样调用@hide和internal API
- TextView的setText注意事项
- [Android] ImageView.ScaleType设置图解
- Android 5.1 添加硬件抽象层(HAL)和JNI接口总结
- android——背景颜色渐变(梯度变化)
- android Pbap下载手机电话簿协议介绍
- android 禁止横竖版切换
- Android 常用工具类
- 12.10 android双向通话录音,ClipData监听,coding.net pymongo连接使用mongo
- 给Android Studio导入第三方库的方法
- android中延迟执行某个任务
- Android 自制的一个简单的心电图pulseview
- Android之使用PopupWindow使用和总结
- 如何从软硬件层面提升 Android 动画性能?
- Android中AlarmManager的使用
- android:scaleType属性解析
- android studio的使用说明
- Android RecyclerView 使用完全解析 体验艺术般的控件