位置: 编程技术 - 正文

Android针对不同屏幕分辨率的4种布局适应方法(android不同module怎么相互调用)

编辑:rootadmin

推荐整理分享Android针对不同屏幕分辨率的4种布局适应方法(android不同module怎么相互调用),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:如何在android系统中完成不同进程之间的通信,android的相对布局,android相关问题,android不同版本特性,android相关问题,安卓大不同,android不同版本特性,android不同版本特性,内容如对您有帮助,希望把文章链接给更多的朋友!

Android针对不同屏幕分辨率的4种布局适应方法(android不同module怎么相互调用)

一、细说layout_weight

目前最为推荐的Android多屏幕自适应解决方案。 该属性的作用是决定控件在其父布局中的显示权重,一般用于线性布局中。其&#;越小,则对应的layout_width或layout_height的优先级就越高,一般横向布局中,决定的是layout_width的优先级;纵向布局中,决定的是layout_height的优先级。 传统的layout_weight使用方法是将当前控件的layout_width和layout_height都设置成fill_parent,这样就可以把控件的显示比例完全交给layout_weight;这样使用的话,就出现了layout_weight越小,显示比例越大的情况。不过对于2个控件还好,如果控件过多,且显示比例也不相同的时候,控制起来就比较麻烦了,毕竟反比不是那么好确定的。 于是就有了现在最为流行的0px设&#;法。看&#;让人难以理解的layout_height=0px的写法,结合layout_weight,却可以使控件成正比例显示,轻松解决了当前Android开发最为头疼的碎片化问题之一。 先看下面的styles(style_layout.xml)?<?xml version="1.0"encoding="utf-8"?><resources> <!-- 全屏幕拉伸--> <style name="layout_full"> <item name="android:layout_width">fill_parent</item> <item name="android:layout_height">fill_parent</item> </style> <!-- 固定自身大小--> <style name="layout_wrap"> <item name="android:layout_width">wrap_content</item> <item name="android:layout_height">wrap_content</item> </style> <!-- 横向分布--> <style name="layout_horizontal"parent="layout_full"> <item name="android:layout_width">0px</item> </style> <!-- 纵向分布--> <style name="layout_vertical"parent="layout_full"> <item name="android:layout_height">0px</item> </style> </resources> 可以看到,layout_width和layout_height两个属性被我封装成了4个style 根据实际布局情况,选用当中的一种,不需要自己设置,看过我前一个ActivityGroup的Demo的同学应该非常熟悉了 然后我的Demo的布局如下(weight_layout.xml)<?xml version="1.0"encoding="utf-8"?><LinearLayout xmlns:android=" style="@style/layout_full" android:orientation="vertical"> <LinearLayout style="@style/layout_vertical" android:layout_weight="1" android:orientation="horizontal"> <View style="@style/layout_horizontal" android:background="#aa" android:layout_weight="1"/> <View style="@style/layout_horizontal" android:background="#aa" android:layout_weight="4"/> <View style="@style/layout_horizontal" android:background="#aa" android:layout_weight="3"/> <View style="@style/layout_horizontal" android:background="#aaaaaa" android:layout_weight="2"/> </LinearLayout> <LinearLayout style="@style/layout_vertical" android:layout_weight="2" android:orientation="vertical"> <View style="@style/layout_vertical" android:background="#ffffff" android:layout_weight="4"/> <View style="@style/layout_vertical" android:background="#aa" android:layout_weight="3"/> <View style="@style/layout_vertical" android:background="#aa" android:layout_weight="2"/> <View style="@style/layout_vertical" android:background="#aa" android:layout_weight="1"/> </LinearLayout></LinearLayout>二、自定义尺寸法 这个是我自己想出来的方法,可能是个比较笨的方法,所以没有多少人提过用这种方法解决自适应的问题。虽然这个方法缺点也很多,但有时候也是个不错的方法。 先看下面这张图 可以看到我定义了两套尺寸文件,我们可以看下其中一个文件?<?xml version="1.0"encoding="utf-8"?><resources><dimen name="height_1_">6px</dimen><dimen name="height_2_">px</dimen><dimen name="height_3_">px</dimen><dimen name="height_4_">px</dimen><dimen name="height_5_">px</dimen><dimen name="height_6_">px</dimen><dimen name="height_7_">px</dimen><dimen name="height_8_">px</dimen><dimen name="height_9_">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen><dimen name="height__">px</dimen> </resources> 这个是values-x文件夹下dimens_height.xml文件中的代码,我把整个高度分成了等分,这是因为大部分屏幕的宽度或高度都是的整数倍(个别特殊的除外),不同的等分在不同的分辨率中设定不同的尺寸&#;。 由于每一套界面都要写一套,所以有些同学可能觉着不太好,不过这个写起来比较简单,而且以后也不用改,所以有时候也可以考虑用一下! 再看我Demo的布局代码(dimen_layout.xml)?<?xml version="1.0"encoding="utf-8"?><LinearLayout xmlns:android=" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <View android:layout_width="@dimen/width__" android:layout_height="@dimen/height__" android:background="#ffcccc" android:layout_margin="@dimen/width_2_"/> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent"> <View android:layout_width="@dimen/width__" android:layout_height="@dimen/height__" android:background="#ccccff" android:layout_margin="@dimen/height_5_"/> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <Button android:layout_width="@dimen/width__" android:layout_height="@dimen/height_5_" android:background="#ccffcc" android:layout_marginBottom="@dimen/height_1_" android:text="5"/> <Button android:layout_width="@dimen/width__" android:layout_height="@dimen/height__" android:background="#ccffcc" android:layout_marginBottom="@dimen/height_1_" android:text=""/> <Button android:layout_width="@dimen/width__" android:layout_height="@dimen/height__" android:background="#ccffcc" android:layout_marginBottom="@dimen/height_1_" android:text=""/> <Button android:layout_width="@dimen/width__" android:layout_height="@dimen/height__" android:background="#ccffcc" android:text=""/> </LinearLayout> </LinearLayout> </LinearLayout> 以上是我写的统一的布局代码,来看下在两个不同分辨率的模拟器上的显示效果吧(大家注意我的代码中有margin这样的&#;也用到了自定义尺寸,如果这个margin使用layout_weight来控制的话,无疑要多嵌套一层线性布局,所以说没有一个方法是十全十美的,这第2个方法有时候用起来反而还要方便一些)三、在java代码中设置宽高度 也许很多人会反对这种方法,因为即使是官方也是推荐使用xml的方式写布局。不过我们在这不会像Swing那样写那么多麻烦的布局代码,因为我们只是在代码中重新设定控件的宽高度而已,其他属性依然是交给xml布局文件的。这个方法其实是我跟同事偷学来的,虽然我不赞成这样的方法,但他确确实实也是解决屏幕自适应问题的方案之一,而且它没我想象的那么复杂,其实很简单。 首先我们要做的是获取当前屏幕的宽高度,因为这个在后面要用到 我们可以写两个静态变量用来保存当前屏幕的宽高度:?1ic classConstant {2 publicstatic int displayWidth; //屏幕宽度3 publicstatic int displayHeight; //屏幕高度4} 然后在第一个Activity启动的时候,获取这两个&#;?1DisplayMetrics displayMetrics = newDisplayMetrics();2 getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);3 Constant.displayWidth = displayMetrics.widthPixels;4 Constant.displayHeight = displayMetrics.heightPixels; 布局代码我们可以全都统一写成wrap-content,其实写成什么都无所谓,因为这个&#;只是暂时的?<?xml version="1.0"encoding="utf-8"?><LinearLayout xmlns: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方法里这么做?// 第一个按钮,宽度%,高度% LinearLayout.LayoutParams params =new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, (int) (Constant.displayHeight * 0.1f &#; 0.5f)); btn1.setLayoutParams(params); // 第二个按钮,宽度%,高度% LinearLayout.LayoutParams params2 =new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, (int) (Constant.displayHeight * 0.3f &#; 0.5f)); btn2.setLayoutParams(params2); // 第三个按钮,宽度%,高度% LinearLayout.LayoutParams params3 =new LinearLayout.LayoutParams( (int) (Constant.displayWidth * 0.5f &#; 0.5f), (int) (Constant.displayHeight * 0.2f &#; 0.5f)); btn3.setLayoutParams(params3); // 第三个按钮,宽度%,高度填满剩下的空间 LinearLayout.LayoutParams params4 =new LinearLayout.LayoutParams( (int) (Constant.displayWidth * 0.7f &#; 0.5f), LayoutParams.FILL_PARENT); btn4.setLayoutParams(params4); 大家可以看到其实代码并不复杂,都能看得懂四、多布局 做为最后的方法,也是最后一个才会考虑的方法,那就是为不同的尺寸界面单独写布局。不到万不得已不要用这个方法,相信不少人和我一样都被&#;着用过这个方法吧。需要说明的是,横竖屏切换使用不同布局也是用这个方法解决的;代码我就不上了,给大家看两张图吧,一个是同1个布局的,一个是写了多布局的,大家一看就明白了补充一下,写多个布局的时候,配置文件一定要加上这段配置代码,不然有时可能会出问题 <supports-screens android:largeScreens="true" android:normalScreens="true" android:anyDensity="true" />五、其他 以上说的都是多个屏幕显示相同内容需要考虑的问题,还有一种是在不同的屏幕上显示内容不同的情况,其实这个问题我们往往是用滚动视图来解决的,也就是ScrowView;需要注意的是ScrowView中使用layout_weight是无效的,既然使用ScrowView了,就把它里面的控件的大小都设成固定的吧。 此外关于图片的自适应问题,主要是2点,一个是9patch图,这个东西大家都要学会去做,不难;不过有些编译器在识别9patch图时会出这样那样的bug,像我的Eclipse就不认这个,而同一个9patch图在别的电脑上却是没问题的, 第二个要说的是我曾经被困扰的一个问题,对于x 和 x这两个尺寸,他们显示同一个图片时,总有一个会拉伸(如果9patch可以解决的还好)。其实当初困扰我的是,这两个尺寸都是hdpi的,以为无法给这两个屏幕做不同的图片。后来无意中发现,图片可以和布局一样分多个尺寸的,而不仅仅是根据密度分,也就是说你可以写这样的文件夹drawable-hdpi-x和drawable-hdpi-x,在它们里面放不同的图片,这样图片也能自适应了。

版权声明:本文为博主原创文章,未经博主允许不得转载。

Android 让EditText失去焦点避免自动弹出输入法 如果一进去activity,EditText就获取焦点,弹出输入法界面,无疑是很影响美观的。可以在其父组件(布局)上添加以下两句代码:android:focusable=trueandroid:focus

[置顶] Android各种轮子 数据库ORMLite框架

ObjectAnimator Demo 这里提供一个ObjectAnimator以供参考:这个Demo基本实现了从左向右(从左边出来,然后停留在中间放大,然后从右边缩小消失的效果)代码比较简单,就

标签: android不同module怎么相互调用

本文链接地址:https://www.jiuchutong.com/biancheng/371037.html 转载请保留说明!

上一篇:记住密码与自动登录功能(记住密码自动登录 会更新登陆信息吗)

下一篇:Android 让EditText失去焦点避免自动弹出输入法

  • 煤炭企业生产能力
  • 小规模纳税人收入是含税还是不含税
  • 商贸公司可以做美容行业吗?
  • 收到银行汇票要去银行办理吗
  • 小规模开专票要计提附加税吗
  • 出纳工人借支与贷款区别
  • 债务清偿如何进行税务处理
  • 事业单位经营结余贷方转入哪里
  • 公司法人要交什么税
  • 视同销售的消费税计入成本吗?
  • 签订设备维修合同会计分录
  • 公司支付宝付款
  • 公司的一些党员特别喜欢健身
  • 公司纳税人是什么意思是不是法人
  • 增值税即征即退政策
  • 小规模纳税人附加税优惠政策2023
  • 2021年沙子水泥税率是多少
  • 增值税代扣代缴抵扣
  • 税务大厅申报社保需要资料
  • 无票收入已报税,后来开票账务处理
  • 费用报销单如何粘贴票据
  • 企业银行基本户
  • 免税农产品加工企业
  • 公司变更名称重新申请
  • 健康检查查询系统
  • 计提长期待摊费用的会计分录
  • 应交增值税缴纳后入什么费用
  • springboot上传文件大小设置
  • 进度条js代码
  • 辅导期内的企业有哪些
  • 政府机关出租房屋要交税吗
  • 房地产消防工程入什么科目
  • 宣告分配现金股利和股票股利的区别
  • 企业所得税年报截止日期2023
  • LangChain与大型语言模型(LLMs)应用基础教程:信息抽取
  • thinkphp框架介绍
  • laravel 5.3中自定义加密服务的方案详解
  • 股东垫付款怎么处理
  • patriotic研究
  • vue3版本网页小游戏
  • React - Redux Hooks的使用细节详解
  • cp命令复制文件到另一个目录并改名
  • reprovision签名失败error
  • 查看zip内容
  • wordpress标签tag文章
  • 为什么要扩展
  • 个人所得税生产经营所得税怎么申报
  • mongodb如何删除
  • sql server 2008 R2安装
  • 上市公司收购其它公司是好是坏
  • 公司支付宝收款码怎么支持信用卡
  • 以前年度生产成本怎么转为研发费用
  • 固定资产改造多少直接进费用
  • 发放外单位人员劳务费需要在发放表上签字吗
  • 收付转三种凭证的限制类型
  • 服务费税额部分计算公式
  • 税务人员如何做好本职工作
  • sql server复制表数据
  • Linux下MySQL 5.6.27 安装教程
  • sql1068错误
  • MySQL 5.7.14 net start mysql 服务无法启动-“NET HELPMSG 3534” 的奇怪问题
  • windowsxp注册表在哪
  • linux服务器宕机排查
  • rsync 教程
  • mac菜单栏不见了
  • linux系统怎么安装wine
  • fsma32.exe进程是什么文件 fsma32是什么进程
  • post installation
  • win7打开回收站
  • 朝鲜引擎
  • [置顶]星陨计划
  • opengl绘制坐标轴
  • 用python编写程序
  • python读取文件指定内容
  • unity游戏加密
  • 广东省电子税务局登录方式
  • 慰问金使用规定
  • 建筑业发票范围
  • 讲话和讲话精神的区别
  • 邳州国税局副局长
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设