钛处理不同的资源

Ant*_*and 9 resolution titanium android-layout

简单的问题,这是确保应用程序在不同的屏幕分辨率下工作而不看废话的最佳方法?我不能使用静态值,那么它需要根据分辨率进行调整.现在我正在使用相对测量(屏幕百分比),但想知道这是否真的是最好的处理方式!?

Tev*_*o D 7

我们成功的另一个/附加选项是一小组功能,它们使用屏幕密度值来计算显示尺寸...这当然只是一个近似值,因为只有四个密度,但我们发现这个非常有帮助.

//=============================================================================
// inch
//
// compute approximate number of pixels for the specified physical on-screen
// size based on the density reported by the OS
//=============================================================================
function inch(size)
{
    // default to 160 dpi if unavailable
    var height = size * 160.0; 

    try
    { 
        // compute header height based on screen density ... target .25" height
        height = size * Ti.Platform.displayCaps.dpi; 
    }
    catch(e) { warn("Error accessing display caps for screen density calculation: " + e); }

    return height;
}
Run Code Online (Sandbox Code Playgroud)

所以,如果你想在屏幕上高达3/4英寸......

Ti.UI.createThing({ height: inch(.75)});
Run Code Online (Sandbox Code Playgroud)

...或者如果你想按点大小来缩放,可以制作一些常量......

const g_12pt = inch(12/72); //0.166667
const g_10pt = inch(10/72); //0.138889
const g_8pt = inch(8/72);   //0.111111
const g_6pt = inch(6/72);   //0.083333
...
...font:{fontSize: g_10pt, fontWeight:"bold"},...
Run Code Online (Sandbox Code Playgroud)

我们还创建了一些功能来获得屏幕的高度和宽度,因此如果我们想要在平板电脑或更小的东西上更好的布局,我们可以更好地理解我们正在处理的内容.

//=============================================================================
// screenWidth - return screen width in inches
//=============================================================================
function screenWidth()
{
    return Titanium.Platform.displayCaps.platformWidth / Titanium.Platform.displayCaps.dpi;
}

//=============================================================================
// screenHeight - return screen height in inches
//=============================================================================
function screenHeight()
{
    return Titanium.Platform.displayCaps.platformHeight / Titanium.Platform.displayCaps.dpi;
}
Run Code Online (Sandbox Code Playgroud)

你可以从那里继续......但这确实帮助我们确定了我们如何在不同的密度和平台上布置我们的屏幕.inch函数只有异常处理,因为我们在应用程序的早期使用它,有时Ti.Platform仍未定义.当我们布置报告时,Ti.Platform可用,因此屏幕功能没有异常处理.如果您需要提前查询,则可能还需要异常处理.

希望这可以帮助!


Sk4*_*446 5

你想要修改你的tiapp.xml - 在android部分你添加一个清单记录,如下所示:

<android xmlns:android="http://schemas.android.com/apk/res/android">
    <manifest>
        <supports-screens android:anyDensity="false"/>
    </manifest>
</android>
Run Code Online (Sandbox Code Playgroud)

这将使应用默认使用与旧版本钛相同的样式,它基本上根据使用的设备进行缩放.有关更改的更多详细信息,请访问:http://developer.appcelerator.com/blog/2011/06/new-defaults-for-android-layouts-in-1-7.html

  • 没问题 - Appcelerator在这类事情上提供的信息量极其糟糕! (3认同)