如何制作可在手机和平​​板电脑中使用的响应式 Android 应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21642005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:52:11  来源:igfitidea点击:

How to make responsive Android application which is used in mobile and also in tablet?

androidandroid-layout

提问by Sagar Zala

I have created one Android application.

我创建了一个 Android 应用程序。

When I run my application in Mobile Phoneit works very well, but when I run in Tabletthe layout of application is changed.

当我在Mobile Phone其中运行我的应用程序时,它工作得很好,但是当我在Tablet其中运行时,应用程序的布局发生了变化。

So, how to make responsive Android application which is used in Mobileand also in Tablet?

那么,如何制作MobileTablet.

回答by Harshal Doshi Jain

On Android we can use screen size selector, introduced from Android 3.2, to define which layout to use. More details available at http://android-developers.blogspot.in/2011/07/new-tools-for-managing-screen-sizes.html. Following code snippet has been extracted from the same link :

在 Android 上,我们可以使用从Android 3.2引入的屏幕尺寸选择器来定义要使用的布局。更多详细信息,请访问http://android-developers.blogspot.in/2011/07/new-tools-for-managing-screen-sizes.html。以下代码片段已从同一链接中提取:

public class MyActivity extends Activity 
{
    @Override protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate();
        Configuration config = getResources().getConfiguration();
        if (config.smallestScreenWidthDp >= 600) 
        {
            setContentView(R.layout.main_activity_tablet);
        } 
        else 
        {
            setContentView(R.layout.main_activity);
        }
    }
}

Another good reference for size configuration is keeping separator. This is explain in details at : http://www.vanteon.com/downloads/Scaling_Android_Apps_White_Paper.pdf

大小配置的另一个很好的参考是保留分隔符。这是详细解释:http: //www.vanteon.com/downloads/Scaling_Android_Apps_White_Paper.pdf

回答by M D

I am only talkin about Mobile Responsive Design. With layouts, I believe you can only current differentiate by the following:

我只是在谈论移动响应式设计。对于布局,我相信您目前只能通过以下方式进行区分:

res/layout/my_layout.xml            // layout for normal screen size
res/layout-small/my_layout.xml      // layout for small screen size
res/layout-large/my_layout.xml      // layout for large screen size
res/layout-large-land/my_layout.xml // layout for large screen size in landscape mode

You can find more info on what you can add to the folder structure to differentiate between different settings Documentationand android-developers.blogspot

您可以找到有关可以添加到文件夹结构中以区分不同设置文档android-developers.blogspot 的更多信息

In order to accommodate other types of tablets and screen sizes android introduces a new way to specify resources for more discrete screen sizes. The new technique is based on the amount of space your layout needs (such as 600dp of width), rather than trying to make your layout fit the generalized size groups (such as large or xlarge).

为了适应其他类型的平板电脑和屏幕尺寸,android 引入了一种新方法来为更离散的屏幕尺寸指定资源。新技术基于您的布局需要的空间量(例如 600dp 的宽度),而不是试图使您的布局适合通用尺寸组(例如 large 或 xlarge)。

Update:There are essentially two ways you can give your audience a good experience utilizing responsive design:

更新:基本上有两种方法可以利用响应式设计为您的观众提供良好的体验:

  1. Optimize the layout of your content.

  2. Adapt the content that's shown.

  1. 优化您的内容布局。

  2. 调整显示的内容。

Update2:Write this code in your activity

更新 2:在您的活动中编写此代码

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
    setContentView(R.layout.landscapeView);

} else {
    setContentView(R.layout.portraitView);
}

And also add this line in your Manifest file

并在您的清单文件中添加这一行

android:configChanges="orientation|keyboardHidden|screenSize"

So this will handle both things, it will not restart your activity and will load the layout as per your orientation changes. For more information go to http://www.codeproject.com/Articles/422431/Handling-screen-layout-changes-in-Android

因此,这将处理这两件事,它不会重新启动您的活动,并将根据您的方向更改加载布局。有关更多信息,请访问http://www.codeproject.com/Articles/422431/Handling-screen-layout-changes-in-Android

回答by Hemal Patel

@Sagar Zala

@萨加尔扎拉

Try to use constraint layout as parent view for your layout which will help to make your application responsive for phone and device.

尝试使用约束布局作为布局的父视图,这将有助于使您的应用程序对手机和设备做出响应。

Constraint Layout

约束布局