1.使用AppUtil类方法
AppUtil类来自于package android.support.think.util;
参考cookie存储数据(使用okhttp框架) - An_nA的博客 - CSDN博客 http://blog.csdn.net/an_nal/article/details/76146598添加jar包,这样才能使用AppUtil类方法。
控件名:
LinearLayout layoutLogin;
代码:
int width = AppUtil.getScreenWidth(this) / 3;
int height = AppUtil.getScreenHeight(this) / 4;
ViewGroup.LayoutParams layoutParams = layoutLogin.getLayoutParams();
layoutParams.height = height;
layoutParams.width = width;
layoutLogin.setLayoutParams(layoutParams);
2.纯代码:
tvAddData为控件的名字:
int width = this.getResources().getDisplayMetrics().widthPixels / 3;
int height = this.getResources().getDisplayMetrics().heightPixels / 4;
ViewGroup.LayoutParams layoutParams = tvAddData.getLayoutParams();
layoutParams.height = height;
layoutParams.width = width;
tvAddData.setLayoutParams(layoutParams);
3.按屏幕比例控制对话框的宽高
对话框布局文件xml中的最外层控件宽高则都需为match_parent:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="@color/colorAccent"
android:textColor="#ffffff"
android:text="99999"
android:textSize="16sp"/>
</LinearLayout>
// 将对话框的大小按屏幕大小的百分比设置
WindowManager m = getWindow().getWindowManager();
Display d = m.getDefaultDisplay();
final WindowManager.LayoutParams p = getWindow().getAttributes();
p.height = (int) (d.getHeight() * 0.8);
p.width = (int) (d.getHeight() * 0.8);
getWindow().setAttributes(p);