android自动适应屏幕三、在java代码中设置宽高度


    也许很多人会反对这种方法,因为即使是官方也是推荐使用xml的方式写布局。不过我们在这不会像Swing那样写那么多麻烦的布局代码,因为我们只是在代码中重新设定控件的宽高度而已,其他属性依然是交给xml布局文件的。这个方法其实是我跟同事偷学来的,虽然我不赞成这样的方法,但他确确实实也是解决屏幕自适应问题的方案之一,而且它没我想象的那么复杂,其实很简单。
    首先我们要做的是获取当前屏幕的宽高度,因为这个在后面要用到
    我们可以写两个静态变量用来保存当前屏幕的宽高度:

?
代码片段,双击复制
01
02
03
04
public class Constant {
         public static int displayWidth;   //屏幕宽度
         public static int displayHeight; //屏幕高度
}

    然后在第一个 Activity 启动的时候,获取这两个值
?
代码片段,双击复制
01
02
03
04
             DisplayMetrics displayMetrics = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
                Constant.displayWidth = displayMetrics.widthPixels;
                Constant.displayHeight = displayMetrics.heightPixels;

    布局代码我们可以全都统一写成 wrap-content,其实写成什么都无所谓,因为这个值只是暂时的  
?
代码片段,双击复制
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?xml version= "1.0" encoding= "utf-8" ?>
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
    android:orientation= "vertical"
    android:layout_width= "fill_parent"
    android:layout_height= "fill_parent" >
<Button  
        android:id= "@+id/btn1"
    android:layout_width= "wrap_content"
    android:layout_height= "wrap_content"
    android:background= "#ffcccc"
    android:text= "aaaaaaaa" />
<Button  
        android:id= "@+id/btn2"
    android:layout_width= "wrap_content"
    android:layout_height= "wrap_content"
    android:background= "#ccffcc"
    android:text= "bbbbbbbbb" />
<Button  
        android:id= "@+id/btn3"
    android:layout_width= "wrap_content"
    android:layout_height= "wrap_content"
    android:background= "#ccccff"
    android:text= "ccccccccc" />
<Button  
        android:id= "@+id/btn4"
    android:layout_width= "wrap_content"
    android:layout_height= "wrap_content"
    android:background= "#ffffcc"
    android:text= "dddddddddddddddddd" />   
</LinearLayout>

    最后我们在 Activity onCreate 方法里这么做  
?
代码片段,双击复制
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
// 第一个按钮,宽度100%,高度10%
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                                LayoutParams.FILL_PARENT,
                                ( int ) (Constant.displayHeight * 0 .1f + 0 .5f));
                btn1.setLayoutParams(params);
                // 第二个按钮,宽度100%,高度30%
                LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
                                LayoutParams.FILL_PARENT,
                                ( int ) (Constant.displayHeight * 0 .3f + 0 .5f));
                btn2.setLayoutParams(params2);
                // 第三个按钮,宽度50%,高度20%
                LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(
                                ( int ) (Constant.displayWidth * 0 .5f + 0 .5f),
                                ( int ) (Constant.displayHeight * 0 .2f + 0 .5f));
                btn3.setLayoutParams(params3);
                // 第三个按钮,宽度70%,高度填满剩下的空间
                LinearLayout.LayoutParams params4 = new LinearLayout.LayoutParams(
                                ( int ) (Constant.displayWidth * 0 .7f + 0 .5f),
                                LayoutParams.FILL_PARENT);
                btn4.setLayoutParams(params4);

    大家可以看到其实代码并不复杂,都能看得懂
    下面是效果显示图

3.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值