Saturday, February 23, 2019

Use dimen to set dimension for view

Android phone have many different resolution screen. When create view it may look too different size in another phones.
To avoid this, we use dimen in tag values.
<dimen name="size">200dp</dimen>
Sau đó trong class, ta lấy chỉ số này như sau.
int ro = (int) getResources().getDimension(R.dimen.size);
For example, we have a view like this.
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
           WindowManager.LayoutParams.WRAP_CONTENT,
           WindowManager.LayoutParams.WRAP_CONTENT,
           WindowManager.LayoutParams.TYPE_PHONE,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                  PixelFormat.TRANSLUCENT);
To set width for view, change to.
params = new WindowManager.LayoutParams( 
            ro, 
            80, 
            LayoutParams.TYPE_PHONE, 
            LayoutParams.FLAG_NOT_FOCUSABLE, 
          PixelFormat.TRANSLUCENT);

If want to set height, do the same.

No comments:

Post a Comment