获取屏幕宽高,按指定比例设置控件的宽高

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);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值