android代码构建布局时设置宽高的单位为像素(图解)
来源:互联网 发布:无线路由密码破解软件 编辑:程序博客网 时间:2024/04/30 01:26
android构建布局时一般通用xml布局来生成,但有时还是需要代码来生成布局控件,以达到不同的效果。以前用过代码生成布局,现在写个总结,方便新手查看。
这里代码来生成布局控件设置的宽高的单位为:像素。
像素即pixel,简写为px,我们平时说的手机的400*800分辨率,这里400*800的单位就是像素。常见的分辨率有如(MX)640*960,小米2S:720*1280,三星galaxy s4:1080*1920
回到正题,以下是我测试几台手机得到的图片,先看代码:
package com.example.dptest;import android.os.Bundle;import android.app.Activity;import android.graphics.Color;import android.util.DisplayMetrics;import android.view.Menu;import android.widget.RelativeLayout;import android.widget.RelativeLayout.LayoutParams;import android.widget.TextView;public class MainActivity extends Activity {private RelativeLayout rl_code;private TextView tv_diaplay_info;private Activity context;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);context = MainActivity.this;setContentView(R.layout.activity_main);rl_code = (RelativeLayout) findViewById(R.id.relativeLayout_code);tv_diaplay_info = (TextView) findViewById(R.id.tv_diaplay_info);//生成代码RelativeLayout rl_code2 = new RelativeLayout(context);LayoutParams params = new LayoutParams(630,100);//魅族的分辨率率为:640*960,这里宽故意设置成630以方便看出差别rl_code2.setLayoutParams(params);rl_code2.setBackgroundColor(Color.YELLOW);rl_code.addView(rl_code2);//添加//以下只是显示当前的机型与分辨率,方便查看图片DisplayMetrics metrics = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(metrics);String info = "当前机型:魅族MX \nwidthPixels(像素宽):"+metrics.widthPixels+"\nheightPixels(像素高):"+metrics.heightPixels;tv_diaplay_info.setText(info);} }
布局文件activity_main.xml里就一个ReletiveLayout,看代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/relativeLayout_code" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" ></RelativeLayout>
魅族MX测试效果图如下:
由图片可以看出相对布局(图中黄色的小块)的宽接近屏幕的边缘(因为我在代码里设置少了10px),说明代码生成控件时,其宽高的单位为像素px
下面上传在其它分辨率的手机测试时的图片,布局的宽都比原手机分辨率小10px,方便看出差别。
下图手机分辨率为:1080*1920
下图分辨率手机为480*800
- android代码构建布局时设置宽高的单位为像素(图解)
- Android 获取屏幕宽高-以像素为单位
- Android中布局的像素和单位
- android 布局方式 像素单位
- android 布局方式 像素单位
- 解决Android中LayoutParam宽高的单位设置问题
- android-getTextSize返回值是以像素(px)为单位的,setTextSize()以sp为单位
- android-getTextSize返回值是以像素(px)为单位的,setTextSize()以sp为单位
- android-getTextSize返回值是以像素(px)为单位的,setTextSize()以sp为单位
- android-getTextSize返回值是以像素(px)为单位的,setTextSize()以sp为单位
- android-getTextSize返回值是以像素(px)为单位的,setTextSize()以sp为单位
- android-getTextSize返回值是以像素(px)为单位的,setTextSize()以sp为单位
- android-getTextSize返回值是以像素(px)为单位的,setTextSize()以sp为单位
- android-getTextSize返回值是以像素(px)为单位的,setTextSize()以sp为单位
- android getTextSize返回值是以像素(px)为单位的,setTextSize()以sp为单位
- android-getTextSize返回值是以像素(px)为单位的,setTextSize()以sp为单位
- 高清晰度的iPhone和iPod touch(单位:像素)
- Android像素单位的理解
- EasyObjects更新所有记录 (BY 冷家锋)
- [转]自定义宏注释模板 vs2008
- Linux Shell 文本文件处理
- A potentially dangerous Request.Form value was detected from the client
- 吊炸天,开源游戏组织线下聚会的结果…… 当Firefly + cocos2d-x + 硬件=?
- android代码构建布局时设置宽高的单位为像素(图解)
- 国际化之locale
- Dom4j 操作, 节点查找 添加 删除 修改 。。。xPath
- Android中SQLite应用详解
- JAVA语言运算符
- AndroidManifest.xml文件详解(uses-configuration)
- SpiderMonkey 入门
- 风起亚洲公共云与VPS比较
- 程序猿自救指南之——如何保护你的腰?