Android 通用方法 [英] Android Universal App Approach

查看:40
本文介绍了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 .

我读过让您的应用为 Jelly Bean 和 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  . 

我们怎样才能做到这一点??在这篇文章中,它说您为不同的屏幕设计了具有相同名称的不同布局并将其保存在相应的文件夹中.但是我怀疑如果它在手机中运行应用程序时尝试初始化平板电脑布局的小部件组件会不会出现异常.

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:我想到的想法是确定我使用的是哪种类型的设备,即 Tab 或手机.

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

确定设备是智能手机还是平板电脑?

如果 app 是 phone 则避免小部件的初始化.还有比这更好的方法吗??

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" 平板电脑.这是我的场景,你也可以定义layout-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.

为此,在值/字符串中,我放置了以下代码,

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

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

平板电脑也一样values-sw600dp/strings

<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天全站免登陆