简体   繁体   中英

How do I call inside the layout activity_main(sw320dp)?

I want to call the same activity on all devices (Mob&TAb)

Layout:- activity_main(sw320dp) Seen this layout image

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main(sw320dp));
 }
}

It's recommended to use resource qualifiers , but there's a way to do it in code.

You could have all the layouts in the same folder (eg folder layout ) with the appropriate prefixes:

  • activity_main_base.xml
  • activity_main_svga.xml
  • activity_main_sw320dp.xml
  • (other layouts…)

Then in the onCreate , you just have to get the one you want:

setContentView(R.layout.activity_main_sw320dp));

Now, if you need to select layouts depending on screen metrics…

by screen size :

   int screenLayout = context.getResources().getConfiguration().screenLayout 
                          &= Configuration.SCREENLAYOUT_SIZE_MASK;

   switch (screenLayout) {
     case Configuration.SCREENLAYOUT_SIZE_SMALL:
        // small
        break;
     case Configuration.SCREENLAYOUT_SIZE_NORMAL:
        // normal
        break;
     case Configuration.SCREENLAYOUT_SIZE_LARGE:
        // large
        break;
     case Configuration.SCREENLAYOUT_SIZE_XLARGE:
        // xlarge
        break;
     default:
        // undefined
        break;
   }

by screen density :

   int density = context.getResources().getDisplayMetrics().densityDpi;

   switch (density) {
     case DisplayMetrics.DENSITY_LOW:
        // low ldpi
        break;
     case DisplayMetrics.SCREENLAYOUT_SIZE_NORMAL:
        // medium mdpi
        break;
     case DisplayMetrics.SCREENLAYOUT_SIZE_LARGE:
        // high hdpi
        break;
     case …:
        // other densities (xhdpi, xxhdpi,…)
        break;
     default:
        // undefined
        break;
   }

You can use some others like screen width or height and combine them as required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM