了解开发手机的各项参数之UI大小

来源:互联网 发布:电钢琴 知乎 编辑:程序博客网 时间:2024/05/16 23:58

上一篇讲了获取显示屏的一些参数,那么今天说下怎么获得系统UI的一些参数.

下面是一个获取状态栏高度的例子.

public class MainActivity extends Activity {private TextView tv;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);try {//利用反射,获取framework层R类中的dimen类Class<?> classT = Class.forName("com.android.internal.R$dimen");//创建对象Object obj = classT.newInstance();//获取"status_bar_height"的索引字段Field field = classT.getField("status_bar_height");//获取"status_bar_height"的索引int id = field.getInt(obj);//获取"status_bar_height"的索引值,单位是pxint pxStatusBar = getResources().getDimensionPixelSize(id);tv = (TextView)findViewById(R.id.show);tv.setText(" "+pxStatusBar);} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InstantiationException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (NoSuchFieldException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

程序执行的结果就是 69,说明我手机的状态栏是69px.


说明:

1.

//利用反射,获取framework层R类中的dimen类

Class<?> classT = Class.forName("com.android.internal.R$dimen");


我们知道,每一个安卓程序都会通过xml文件,生成一个R类,然后里面有String,id,layout等等静态类,每个子类

里面,就是相应的public static的字段.所以,只要我们知道了包名,就能获得该apk中绝大部分控件的大小.

也就是说,com.android.internal.R$dimen可以替换成别的apk的包名.


2.

//获取"status_bar_height"的索引字段
Field field = classT.getField("status_bar_height");

这里的"status_bar_height"是从哪里获得的?

Android 2.3.5源代码 更新至android 4.4,可以下载,度娘网盘

这里下载4.4.2的系统源码吧, 3.81G,记得解压到某个盘的根目录,否则可能文件夹名称长度超过限制导致部分文件失败.

mydroid\frameworks\base\core\res\res\values\dimen.xml文件

<resources>    <!-- The width that is used when creating thumbnails of applications. -->    <dimen name="thumbnail_width">164dp</dimen>    <!-- The height that is used when creating thumbnails of applications. -->    <dimen name="thumbnail_height">145dp</dimen>    <!-- The standard size (both width and height) of an application icon that         will be displayed in the app launcher and elsewhere. -->    <dimen name="app_icon_size">48dip</dimen>    <!-- The maximum number of action buttons that should be permitted within         an action bar/action mode. This will be used to determine how many         showAsAction="ifRoom" items can fit. "always" items can override this. -->    <integer name="max_action_buttons">2</integer>    <dimen name="toast_y_offset">64dip</dimen>    <span style="color:#cc0000;"><!-- Height of the status bar -->    <dimen name="status_bar_height">25dip</dimen></span>    <!-- Height of the bottom navigation / system bar. -->    <dimen name="navigation_bar_height">48dp</dimen>    <!-- Height of the bottom navigation bar in portrait; often the same as @dimen/navigation_bar_height -->    <dimen name="navigation_bar_height_landscape">48dp</dimen>    <!-- Width of the navigation bar when it is placed vertically on the screen -->    <dimen name="navigation_bar_width">42dp</dimen>    <!-- Height of notification icons in the status bar -->    <dimen name="status_bar_icon_size">24dip</dimen>    <!-- Size of the giant number (unread count) in the notifications -->    <dimen name="status_bar_content_number_size">48sp</dimen>    <!-- Height of the system bar (combined status & navigation); used by         SystemUI internally, not respected by the window manager. -->    <dimen name="system_bar_height">@dimen/navigation_bar_height</dimen>


可以看到,这里有很多属性,包括:

<!-- Height of the status bar -->

<dimen name="status_bar_height">25dip</dimen>


然后:

int pxStatusBar = getResources().getDimensionPixelSize(id); = 69

之前的文章说过,我的屏幕密度是2.75, 所以 25*2.75 = 68.85.

如果用这一行:

float pxStatusBar = getResources().getDimension(id); = 68.85

因为我这个代码是谷歌的,有些手机会改这些默认值,不过看来我手机用的是默认的,没有改过.


总的来说,反射是个好东西,我这里仅仅是举了一个简单的例子,如果深入源码去发掘的话,还能做出更多有意思的东西.



0 0
原创粉丝点击