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

前端 未结 3 1444
孤独总比滥情好
孤独总比滥情好 2020-12-24 11:13

I have created one Android application.

When I run my application in Mobile Phone it works very well, but when I run in Tablet the layout

相关标签:
3条回答
  • 2020-12-24 11:47

    @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

    0 讨论(0)
  • 2020-12-24 11:48

    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 Documentation and 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).

    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.

    Update2: Write this code in your activity

    @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

    0 讨论(0)
  • 2020-12-24 11:51

    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 :

    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

    0 讨论(0)
提交回复
热议问题