<分区>
我正在开发 320*480 分辨率的应用程序。如何让应用程序在 480*854 屏幕上运行?当我尝试在 480*854 屏幕上运行时,应用程序的原始设计看起来很小。我想为 android 中的每个屏幕创建单独的布局吗?如果是这样,请向我提供示例提示以继续。
<分区>
我正在开发 320*480 分辨率的应用程序。如何让应用程序在 480*854 屏幕上运行?当我尝试在 480*854 屏幕上运行时,应用程序的原始设计看起来很小。我想为 android 中的每个屏幕创建单独的布局吗?如果是这样,请向我提供示例提示以继续。
最佳答案
我已经实现了我自己的处理多个屏幕分辨率的方式。
您当然可以通过在运行时根据 Percentage
设置LayoutParams
来避免这个问题
问题 只发生在 Views/Layouts
有一些恒定的 width 或 height 让我们说 280dp
。 解决方案 非常简单,如果我们以百分比 的形式以编程方式设置我们的Views/Layouts
的布局参数并且只使用常量width 或 height 必要时,在其他地方尝试使用 match_parent
填充空白或使用 weights
并定义每个 View
相对于其他 View
这将帮助您的布局在几乎所有屏幕分辨率下看起来都不错
这是一个示例 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>
注意:我使用px作为固定尺寸布局的宽度/高度,因为在LayoutParams layoutParams = new LayoutParams(int width , int height);
width
和 height
取值为像素
这里是设置宽度和高度百分比的示例代码
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);
}
});
现在可能有些人想知道这里发生了什么
float widthInPercentage = ( (float) 280/320 ) * 100
让我解释一下 280 是我的 LinearLayout
的宽度,320 是我的设备屏幕的宽度(我正在开发),我知道目前我正在测试分辨率为 320 x 480 的设备,我正在做的是计算我的布局覆盖了多少 area < strong>百分比 然后
int mLayoutWidth = (int) ( (widthInPercentage * deviceWidth)/100 )
这里我根据屏幕分辨率计算布局的新宽度,这样您的Views/Layouts
将在每个屏幕分辨率 上看起来完全相同.
结论:如果您需要为您的Views/Layouts
设置一些常量width/height,请始终以px< 为单位设置值/strong> 在布局文件(即 xml)中,然后以编程方式设置 LayoutParams
。
给 Google Android 工程师的建议,我想你们应该认真考虑将 dp/dip
单位更改为 percentage
关于android - 多屏幕分辨率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7156752/