Android的通用应用程序方法 [英] Android Universal App Approach

查看:105
本文介绍了Android的通用应用程序方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发一个通用的应用程序。我是新手,这种做法。那意味着为平板电脑和手机应用程序的单个APK。我不得不经历

I want to develop an universal app . I am novice to this approach .That means a single apk for both tablet and phone application . I had go through

支持库

片段

我的目标是建立一个单一的APK里面不同的UI为平板电脑和电话。

My aim is to build different UI for Tablet and Phone inside a single APk .

我读<一href="http://android-developers.blogspot.in/2012/07/getting-your-app-ready-for-jelly-bean.html">Getting您的应用程序准备果冻豆和Nexus 7 。本文提到的

res/layout/activity_home.xml

To take advantage of the extra space on the 7" screen you might provide an alternative layout:

res/layout-sw600dp/activity_home.xml

The sw600dp qualifier declares that these resources are for devices that have a screen with at least 600dp available on its smallest side. 

    Furthermore you might even provide a different layout for 10" tablets:

res/layout-sw720dp/activity_home.xml

这意味着我们可以使用不同的布局不同的设备。这混淆了我

That means we can use different layout for different device . This confuses me

修改1 ::场景

假设

if my phone UI layout contains one-view pager and 

tablet UI layout contain two-view pager  . 

我们如何才能做到这一点?在这篇文章中,它说,你的designe不同的布局具有相同名称不同的屏幕,并保持相应的文件夹中。但我怀疑这会出现异常,如果它试图在应用程序在手机上运行初始化平板布局部件组成部分。

How can we achieve this ?? In this article it says that you an designe different layout with same name for different screen and keep it corresponding folder . But my doubt will this arise exceptions if it try to initialize widget component of Tablet layout when app is running in a phone .

编辑2: 这个想法在我的心里是确定哪些类型的设备是我使用IE标签或电话。

EDIT 2 : The idea came into my mind is determine which type of device is i am using ie Tab or phone .

<一个href="http://stackoverflow.com/questions/9279111/determine-if-the-device-is-a-smartphone-or-tablet">Determine如果该设备是智能手机或平板电脑?

然后避免部件的初始化,如果应用程序是手机。有没有更好的办法比这??

Then avoid the initialization of widgets if app is phone . Is there any better way than this ??

修改3: 我的应用程序支持,从2.3到更高版本

EDIT 3 : My application support from 2.3 to higher versions

如果我的布局为平板电脑拥有额外的部件相比,手机布局。怎么了我初始化和使用。希望各位理解我的需要。所以,请澄清我的疑问

If my layouts for tablet holds additional widgets compared to phone layout .How an i initialize and use . Hope all understood my need . So please clarify my doubt

推荐答案

当我面对这个问题,我创建了如下的结构。

When I face the problem, I create following structure.

res/layout
res/layout-sw600dp

再区分价值和其他资源,

then to distinguish values and other resources,

res/values
res/values-sw600dp
res/values-sw720dp

您注意到,只有一种布局目录和两个值目录中指定的利润率和填充和其他资源。所以单布局可用于7以及10片剂。这是我的情况下,您还可以定义的布局sw720dp 。我这样做,由于减少布局的编译时间。

You noticed that there is only one layout directory and two values directory to specify margins and paddings and other resources. So single layout can be used for 7" as well 10" tablet. This is my scenario, you can also define layout-sw720dp. I did that due to reduce compilation time of layouts.

我也曾经在手机和平​​板电脑不同的布局。例如,我有一个的ListView 在一个屏幕上,然后当上项目的用户点击,它会打开其他的活动,并有的DetailView 为。但在平板电脑,我左侧的的ListView ,右侧的的DetailView

I have also different layouts in phone and tablet. For example, I have a ListView in first screen, then when user click on item, it will open other activity and there is DetailView for that. But in tablet, I have left side ListView and right side DetailView.

因此​​,要做到这一点,在值/字符串,我把下面的code,

So to do so, in values/strings, I place following code,

<bool name="isTablet">false</bool>

和相同的平板的价值观sw600dp /串

<bool name="isTablet">true</bool>

现在,来编码的一部分。我有一个闪屏并具有共同的布局。因此,它会显示常见的屏幕。但是,当任何按钮的用户点击,它会检查它是否是或没有。要检查它,

Now, come to part of coding. I have a splash screen and which has common layout. So it will display common screen. But when user click on any button, it will check whether it is tablet or not. To check it,

boolean isTablet = getResources().getBoolean(R.bool.isTablet);

您现在有标志表明您的应用程序是否正在运行的电话

You have now flag indicate whether your application is running on phone or tablet.

我已经创建了两个包,

com.phone
com.tablet

然后按标志,我就将我的活动为电话包和包。

then as per flag, I direct my activity to phone package and tablet package.

例如:

if(isTablet)
    startActivity(this,TabXYZ.class);
else
    startActivity(this,PhXYZ.class);

而这种做法已经解决了我的问题。

And this approach has solved my problem.

这篇关于Android的通用应用程序方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆