我正在开发的320 * 480的应用程序。如何使应用程序在480 * 854的屏幕上运行?当我试图在480 * 854的屏幕上运行,应用程序最初的设计看起来像小。难道我要创建单独的布局android系统中的每个屏幕?如果是的话,请给我提供样品提示进行。
I am developing the app in 320*480. How do make the app to be run in 480*854 screen? When i tried to run in 480*854 screen, the app original design looks like small. Do I want to create separate layouts for each screen in android? If so, please provide me the sample hint to proceed.
推荐答案
我已经实现了我自己的路处理多种屏幕分辨率
I have implemented my own way of Handling Multiple Screen Resolutions.
您当然可以在百分比
在问题只发生在查看/布局
有一些不变的宽度或高可以说 280dp
。 解决方案很简单,如果我们以编程方式的百分比设置了查看/布局
的布局PARAMS 并只使用不变宽度或高在必要时,其他地方尝试使用 match_parent
来填补空白或使用权重
,定义每一个查看
相对于其他浏览
这将有助于你的布局看几乎在每一个屏幕分辨率好
The Problem occurs only with Views/Layouts
that have some constant width or height lets say 280dp
. Solution is quite simple if we programmatically set Layout Params of our Views/Layouts
in terms of Percentage and only use constant width or height where necessary, elsewhere try to use match_parent
to fill up empty space or use weights
and define every View
relative to other Views
this will help your layout to look good in almost every screen resolutions
下面是一个示例XML
Here is a sample xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/mLayout"
android:layout_width="280px"
android:layout_height="300px" />
</RelativeLayout>
请注意:我在的LayoutParams的LayoutParams =新的LayoutParams使用的 PX 作为固定大小布局的宽/高,因为(INT宽度,诠释高);
的宽度
和高度
取值像素
Notice: I have used px for fixed sized layout's width/height because in LayoutParams layoutParams = new LayoutParams(int width, int height);
the width
and height
take value as pixels
下面是设置的宽度和高按百分比计算的例子code
Here is an example code of setting width and height in terms of percentage
final ViewTreeObserver mLayoutObserver = mLayout.getViewTreeObserver();
mLayoutObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
@Override
public void onGlobalLayout()
{
DisplayMetrics metrics = getResources().getDisplayMetrics();
int deviceWidth = metrics.widthPixels;
int deviceHeight = metrics.heightPixels;
float widthInPercentage = ( (float) 280 / 320 ) * 100;
float heightInPercentage = ( (float) 300 / 480 ) * 100;
int mLayoutWidth = (int) ( (widthInPercentage * deviceWidth) / 100 );
int mLayoutHeight = (int) ( (heightInPercentage * deviceHeight) / 100 );
LayoutParams layoutParams = new LayoutParams(mLayoutWidth, mLayoutHeight);
mLayout.setLayoutParams(layoutParams);
}
});
现在可能有一些人想知道这里发生了什么。
Now might be some people are wondering what is happening here
浮动widthInPercentage =((浮点)280/320)* 100
让我来解释一下的 280 是宽度我的的LinearLayout
和 320 是我的设备屏幕的宽度(关于这一点我正在开发),我现在知道我是测试一个具有设备分辨率的 320×480 ,我在做什么是计算有多少区域我的布局涵盖而言百分比,然后
Let me explain 280 is the width of my LinearLayout
and 320 is the width of my device screen(on which i'm developing), i know currently i'm testing on a device having resolution 320 x 480, what i'm doing is calculating how much area my layout is covering in terms of percentage and then
INT mLayoutWidth =(INT)((widthInPercentage * deviceWidth)/ 100)
在这里,我根据屏幕分辨率,并通过这样的方式计算为我布置的新宽度的查看/布局
看起来完全一样在每一个屏幕分辨率
here i'm calculating the new width for my layout according to the screen resolution and by this way your Views/Layouts
will look exactly the same on every screen resolution.
结论:如果您需要设置一些常数的宽/高作为你的查看/布局
总是设定值在 PX 在布局文件(如XML),然后编程设置的LayoutParams
。
Conclusion: If you need to set some constant width/height for your Views/Layouts
always set value in px in layout file (i.e xml) and then programmatically set LayoutParams
.
一个建议的谷歌Android工程师,我想你们应该认真思考改变的DP / DIP
单位百分比
A Suggestion for Google Android Engineers, i guess you guys should seriously think of changing the dp/dip
units to percentage
相关推荐
最新文章