项目中经常要用到屏幕分辨率来计算控件尺寸来适应不同的屏幕,所以写一下记录下
直接上布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/af" android:orientation="vertical" > <TextView android:id="@+id/id_txt_hello" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:textColor="#fff" /> <TextView android:id="@+id/id_txt" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/hah" android:text="@string/hello" android:textColor="#000" /> </LinearLayout>
直接上代码:
public class GetStatusBarHeightDemoActivity extends Activity { private Window window_; /**当前界面根view*/ private View view_boot_; private TextView txt_hello_; private TextView txt_; /**屏幕宽度*/ private int screenWidth_; /**屏幕高度*/ private int screenHeight_; /**状态栏高度*/ private int statusBarHeight_; /**标题栏高度*/ private int titleBarHeight_; /**App页面高度*/ private int contentHeight_; /**App页面宽度*/ private int contentWidth_; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_get_status_and_title_height); txt_hello_ = (TextView)findViewById(R.id.id_txt_hello); txt_ = (TextView)findViewById(R.id.id_txt); Display display = getWindowManager().getDefaultDisplay(); screenWidth_ = display.getWidth(); screenHeight_ = display.getHeight(); window_ = getWindow(); /*取得系统当前显示的 view根(它是一个framelayout对象)*/ view_boot_ = window_.findViewById(Window.ID_ANDROID_CONTENT); txt_hello_.post(new Runnable() { public void run() { /*获取相关参数*/ getRelatedAttributeValue(); } }); } /** * 获取相关属性值 */ private void getRelatedAttributeValue(){ /*定义一个区域*/ Rect frame = new Rect(); /*区域范围为该textview的区域范围*/ txt_hello_.getWindowVisibleDisplayFrame(frame); /*获取状态栏高度。因为获取的区域不包含状态栏*/ statusBarHeight_ = frame.top; /*获取除了状态栏和标题内容的起始y坐标,也就是 状态栏+标题栏的高度*/ int contentTop = view_boot_.getTop(); /*一减得出标题栏高度*/ titleBarHeight_ = contentTop - statusBarHeight_; /*获取应用内容页面的宽度和高度*/ contentHeight_ = view_boot_.getHeight(); contentWidth_ = view_boot_.getWidth(); txt_.setText("屏幕宽度=" + screenWidth_ + "\n屏幕高度=" + screenHeight_ + "\n状态栏高度=" + statusBarHeight_ + "\n标题栏高度=" + titleBarHeight_ + "\n应用页面高度=" + contentHeight_ + "\n应用页面宽度=" + contentWidth_); } }
如果感觉这个方法获取到0,还可以参照网上的一个办法,用反射来获取:
Class<?> c = null; Object obj = null; Field field = null; int x = 0, sbar = 0; try { c = Class.forName("com.android.internal.R$dimen"); obj = c.newInstance(); field = c.getField("status_bar_height"); x = Integer.parseInt(field.get(obj).toString()); sbar = getResources().getDimensionPixelSize(x); } catch (Exception e1) { e1.printStackTrace(); }
这个办法在自定义控件里也很好用。
源代码:
http://download.csdn.net/detail/zoeice/4401559
本文转自http://blog.csdn.net/zoeice/article/details/7703288 稍微完善下自己想要的内容,测试无误;
2013-1-30总结
首先,有一点需要声明,其实在android中,画布Canvas的高宽其实是屏幕的高宽。
如此一来,获得屏幕高宽的方法就很多了:
1.
WindowManager windowManager = getWindowManager(); Display display = windowManager.getDefaultDisplay(); int screenWidth = display.getWidth(); int screenHeight = display.getHeight();
2.
DisplayMetrics dm = new DisplayMetrics(); this.getWindowManager().getDefaultDisplay().getMetrics(dm);//this指当前activity screenWidth =dm.widthPixels; screenHeight =dm.heightPixels;
以上两种方法在屏幕未显示的时候,还是处于0的状态,即要在setContentView调用之后才有效。
3.还可以在onDraw中由canvas来获得
screenWidth =canvas.getWidth();
screenHeight =canvas.getHeight();
而视图的高宽则是大多数人所认为的canvas的高宽,其实他所指的是除了任务栏和状态栏的高宽,主要有如下方法:
1.
viewWidth=this.getWidth(); viewHeight=this.getHeight();
2.重写view的onSizeChanged,该方法会在onCreate之后,onDraw之前调用
protected void onSizeChanged(int w, int h, int oldw, int oldh) { viewHeight=h; viewWidth=w; super.onSizeChanged(w, h, oldw, oldh); }
本文转自:http://yuyanshan.iteye.com/blog/750043