layout management in android

后端 未结 3 679
长发绾君心
长发绾君心 2021-01-03 05:43

I have created an app for all screen resolutions. So for that, according to the documentation I have created a list of resource directories in an application that provides d

相关标签:
3条回答
  • 2021-01-03 06:20

    i have the same problem, i used another approach. get device height/width :

    DisplayMetrics displayMetrics = new DisplayMetrics();
        WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE); // the results will be higher than using the activity context object or the getWindowManager() shortcut
        wm.getDefaultDisplay().getMetrics(displayMetrics);
        screenWidth = displayMetrics.widthPixels;
        screenHeight = displayMetrics.heightPixels;
    

    and check the device height/width according to that i used layout like

    if(screenWidth == 320 || screenHeight== 480){
            setContentView(R.layout.test_320_480);
        }else if(screenWidth == 240 || screenHeight == 320){
            setContentView(R.layout.test_240_320);
        }else if(screenWidth == 480 || screenHeight == 800 || screenHeight == 854){
            setContentView(R.layout.test_480_800);
        }
    

    use the same id for each controls in the layout for same screen.i hv put all layout in layout folder and it works for me.

    0 讨论(0)
  • 2021-01-03 06:26

    Dont prepare xml for each layouts. just make images for all (ldp, mdpi , hdpi) with same name and put in this different folder as per size.

    ldpi : 240X320
    mdpi : 320X480
    hdpi : 480X800
    

    And give permission in android Android Manifest file.

     <supports-screens android:normalScreens="true"
            android:anyDensity="true" android:largeScreens="true"
            android:smallScreens="true" />
    

    i am apply this way to make universal app in android.it will work fine, try like this way..

    0 讨论(0)
  • 2021-01-03 06:31

    @hasMukh has pointed the right direction, but if you want the layouts for those 4 devices to be more precise. I suggest you to use the "layout-density-resolution" format for layout folder naming. As an example,

    for HVGA(320x480) layout folder should be "layout-hdpi-480x320" for WQVGA(240x400) layout folder should be "layout-ldpi-400x240" for WVGA(480x800) layout folder should be "layout-mdpi-800x480" for WXGA(720x1280) layout folder should be "layout-mdpi-1280x720"

    This method is only good if you are planning to target specific few devices..and you can keep only one drawabale folder and use dp values for layouts.

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