Android 动态创建控件并设置控件的大小之Android屏幕适配攻略(五)

Android 屏幕适配攻略(五)动态创建控件并设置控件的大小

题记
—— 执剑天涯,从你的点滴积累开始,所及之处,必精益求精,即是折腾每一天。

重要消息


例如在 320 * 480 尺寸为 3.2 英寸的手机 ,对应的像素密度应该为 160dpi 对应的像素比例应该是 1.0也就是 1dp = 1sp = 1px

而在 480 * 1280 尺寸为 4.7英寸的手机中,对应的像素密度为 320dpi 对应的像素比例应该是2.0 也就是 1dp = 1sp = 2px

所以在设置控件的尺寸相关时,应当动态的根据屏幕的像素密度大小来计算

下面的实例中都是在 480 * 1280 尺寸为 4.7英寸 的手机中的效果


DisplayMetrics display = new DisplayMetrics();
//将当前窗口的一些信息放在DisplayMetrics类中,
this.getWindowManager().getDefaultDisplay().getMetrics(display);

//获取缩放比例 480 * 1280 尺寸为 4.7英寸 为2.0
float scaledDensity = display.scaledDensity;



1 设置已添加到布局文件中的控件来设置大小
1.1 设置宽度与高度以及内边距
//缩放比例 480 * 1280 尺寸为 4.7英寸 为2.0
//获取TextView
TextView mTvMainShow = (TextView) findViewById(R.id.tv_main_show);

//获取TextView对应的LayoutParams
LayoutParams layoutParams = mTvMainShow.getLayoutParams();

if (layoutParams != null) {

    //设置宽度
    layoutParams.width = (int) (120 * scaledDensity);
    //设置高度
    layoutParams.height = (int) (40 * scaledDensity);

}

//设置内容的内左边距 为20dp 
mTvMainShow.setPadding((int) (20 * scaledDensity),0,0,0);


1.2 设置宽度与高度、内边距 以及外边距

//获取TextView
TextView mTvMainShow = (TextView) findViewById(R.id.tv_main_show);

//获取TextView对应的LayoutParams
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) mTvMainShow.getLayoutParams();

if (layoutParams != null) {

    //设置宽度
    layoutParams.width = (int) (120 * scaledDensity);
    //设置高度
    layoutParams.height = (int) (40 * scaledDensity);

    //设置外左边距
    layoutParams.setMargins((int) (20 * scaledDensity),0,0,0);

}

//设置内容的内左边距 为20dp
mTvMainShow.setPadding((int) (20 * scaledDensity),0,0,0);


需要注意的是,在这里,设置控件的外边距就必须指定 它的layoutParams的类型,例如我这里对应LinearLayout.LayoutParams

因为在设置其外边距的时候需要参考父布局,我这里的TextView是在一个线性布局容器中

布局容器名称对应layoutParams
线性布局 LinearLayoutLinearLayout.LayoutParams
相对布局 LinearLayoutRelativeLayout.LayoutParams
帧布局 LinearLayoutFrameLayout.LayoutParams
表格布局 LinearLayoutTableLayout.LayoutParams

2 动态的创建新的控件并设置尺寸与添加到视图中
//缩放比例 480 * 1280 尺寸为 4.7英寸 为2.0

LinearLayout mLlPrent = (LinearLayout) findViewById(R.id.ll_prent);

//创建TextView
TextView textView = new TextView(this);

//设置TextView的大小
//创建LayoutParams
//参数一 对应的宽度大小
//参数二 对应的高度大小
LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams((int) (120 * scaledDensity), (int) (40 * scaledDensity));

//设置外边距
//参数一 设置左外边距
//参数二 设置顶部外边距
//参数三 设置右外边距
//参数四 设置底部外边距
layoutParams1.setMargins((int) (40 * scaledDensity),(int) (40 * scaledDensity),0,0);

//设置内边距
textView.setPadding(0,0,0,0);

//设置大小
textView.setLayoutParams(layoutParams1);
//设置背景
textView.setBackgroundColor(Color.GRAY);
//添加到布局文件中
mLlPrent.addView(textView);

2.1 对应的xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
              android:layout_width = "match_parent"
              android:layout_height = "match_parent"
              android:gravity = "center_vertical"
              android:id="@+id/ll_prent"
              android:orientation = "vertical" >


        <TextView android:id = "@+id/tv_main_show"
                  android:layout_width = "100dp"
                  android:text="test"
                  android:gravity="center_vertical"
                  android:textSize="14sp"
                  android:textColor="#fff"
                  android:layout_height = "44dp"
                  android:background = "#294881" />
</LinearLayout >

完毕

在这里插入图片描述
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

早起的年轻人

创作源于分享

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值