【HELP】如何Android单套资源文件适应大部分屏幕分辨率

oathevil 2013-07-26 05:13:08
最近做一个Android项目(全仿别人已做好的apk),功能都做好了,就差界面没办法适应多种屏幕分辨率。对原本的apk进行了反编译,发现它只有一套drawable-mdpi的图片资源,但很奇怪的是它的apk几乎可以适应所有的屏幕分辨率。小中屏幕的时候适应得挺好,在大屏幕的情况下,图片有拉伸的效果,这拉伸还是在可接受的范围内的,整体还是保持着界面原来的风格,只是看起来放大了。
在网上找了跟Android UI相关方面的很多资料,都是说要为drawable-ldpi, drawable-mdpi,drawable-hdpi等等各提供一套资源文件之类(谁都晓得多加一套资源文件apk的大小可要大上好多)。仔细分析了反编译后的apk,单从界面UI文件(.xml)来看,并没看出什么特别的地方。它的res资源目录:

***drawable-mdpi
* ...
* ... // 图片资源
***layout
* listtext.xml // 其他界面
* parserddns.xml // 其他界面
* settings.xml // 其他界面
* suffull.xml // 其他界面
* uimain.xml // 主界面
*
***values
* arrays.xml
* ids.xml
* public.xml
* strings.xml
*
***values-en
* arrays.xml
* strings.xml
*
***values-zh-rCN
* arrays.xml
* strings.xml
*
***values-zh-rTW
* arrays.xml
* strings.xml
*
***xml
ddbssetting.xml // 其他界面
preferences.xml // 其他界面

没有发现什么特别的设置,主要都在uimain.xml这个文件里面:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:orientation="horizontal" android:id="@+id/bottom" android:background="@drawable/botton_background" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true">
<Button android:id="@+id/btn_linear_left" android:background="@drawable/linear_left" android:layout_width="wrap_content" android:layout_height="wrap_content" />
...
</LinearLayout>
<LinearLayout android:gravity="center" android:orientation="vertical" android:id="@+id/midle" android:background="@drawable/bottom_midle" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_above="@id/bottom">
<LinearLayout android:orientation="horizontal" android:id="@+id/control_linear1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="3.0dip">
<Button android:id="@+id/btn_control_up" android:background="@drawable/control_up" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="9.0dip" />
...
</LinearLayout>
<LinearLayout android:orientation="horizontal" android:id="@+id/control_linear2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="2.0dip">
<Button android:id="@+id/btn_control_down" android:background="@drawable/control_down" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="9.0dip" />
...
</LinearLayout>
</LinearLayout>
<TextView android:textSize="20.0px" android:gravity="center" android:id="@+id/ip_text" android:background="@drawable/text_background" android:layout_width="fill_parent" android:layout_height="40.0px" />
<TextView android:textSize="15.0px" android:id="@+id/help_msg" android:background="@drawable/text_midle" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_above="@id/midle" />
<com.xxx.xxx.VideoView android:id="@+id/xx_view" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginBottom="5.0dip" android:layout_above="@id/help_msg" android:layout_below="@id/ip_text" />
</RelativeLayout>


我程序里面用的也是它的布局文件,几乎所有资源也都用它的,但就是没法做以像它那像单套drawable-mdpi资源自适应。不知道它是否在代码里面做了什么(因为反编译出来后是smali中间语言,所以没看)。

补充一下,它的布局文件是按 3.2in QVGA 320x480:mdpi 去做的
原版的apk没有效果没有问题,我自己的apk在
4.0in 480x854 下基本正常(有非常细微的错位)
4.3in 720x1280 下错位情况非常明显


请高人解答一下如何利用单套的drawable-mdpi图片资源做到不同屏幕分辨率自适应(先不考虑图片拉伸后效果如何,比例能保持就好)?

...全文
233 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
2在路上 2013-08-13
  • 打赏
  • 举报
回复
获取屏幕宽高,按比例来设置控件高度
gesanri 2013-08-11
  • 打赏
  • 举报
回复
布局中控件的位置不要用绝对坐标,尽量用相对位置来控制,图片用scaleType属性来控制,不考虑拉伸可以设置为fitXY
都是阳光 2013-08-11
  • 打赏
  • 举报
回复
对于你说的我前几天也遇到了图片的问题在代码里面要控制图片比例
oathevil 2013-08-09
  • 打赏
  • 举报
回复
最后发现了是AndroidMainfest.xml里面的<uses-sdk android:minSdkVersion="1" />在作怪,发现在值为1,2,3的时候都自适应, 但在8或者8以后的都会错位。 (注:4、5、6、7没有尝试) 查了下原因,大概是, 随着Android版本的更新,有些界面的风格也开始变化。如果想保持早期低版本时的风格,那么minSdkVersion就应该被设置为相应的sdk版本。 我想这差不多是较为合理的解释了,希望对初学者会有帮助。
a_reader 2013-07-27
  • 打赏
  • 举报
回复
参考下吧,个人感觉layout_weight="1"等分的时候有用,其他时候就不太好控制了。 另外textsize用px做单位文本大小也没法拉伸,用sp比较好。 <LinearLayout android:id="@+id/inputbar_others_container" android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:id="@+id/swich_to_symbol" android:background="@drawable/inputbar_btton_bg" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:layout_alignParentBottom="true" android:clickable="true"/> <ImageButton android:id="@+id/inputbar_setting" android:background="@drawable/inputbar_btton_bg" android:src="@drawable/setting" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:layout_alignParentBottom="true" android:clickable="true"/> <ImageButton android:id="@+id/inputbar_tohand" android:background="@drawable/inputbar_btton_bg" android:src="@drawable/hand" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:layout_alignParentBottom="true" android:clickable="true"/> <ImageButton android:id="@+id/inputbar_speech" android:background="@drawable/inputbar_btton_bg" android:src="@drawable/mic" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:layout_alignParentBottom="true" android:clickable="true"/> <ImageButton android:id="@+id/inputbar_close" android:background="@drawable/inputbar_btton_bg" android:src="@drawable/to_hard_ime" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:layout_alignParentBottom="true" android:clickable="true"/> </LinearLayout>
微工程 2013-07-27
  • 打赏
  • 举报
回复
用 weight,来设定,按照比例分配
oathevil 2013-07-26
  • 打赏
  • 举报
回复
有朋友能帮忙解答这个问题吗

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧