直接看代码
1 public class MainActivity extends Activity { 2 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_main); 7 8 /* Fragment1 fragment1 = new Fragment1(); 9 Fragment2 fragment2 = new Fragment2(); 10 11 FragmentManager fragmentManager = getFragmentManager(); 12 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 13 fragmentTransaction.add(R.id.mainLayout, fragment1, "fragment1"); 14 fragmentTransaction.add(R.id.mainLayout, fragment2, "fragment2"); 15 16 fragmentTransaction.commit();*/ 17 18 Point size = new Point(); 19 this.getWindowManager().getDefaultDisplay().getSize(size); 20 int width = size.x; 21 int height = size.y; 22 if (width > height) {//比较显示窗口的长宽 23 Fragment1 fragment1 = new Fragment1(); 24 getFragmentManager().beginTransaction().replace(R.id.mainLayout, fragment1).commit(); 25 } else { 26 Fragment2 fragment2 = new Fragment2(); 27 getFragmentManager().beginTransaction().replace(R.id.mainLayout, fragment2).commit(); 28 } 29 } 30 }
另外如下方法已经被谷歌deprecated
1 Display display = getWindowManager().getDefaultDisplay(); 2 if (display.getWidth() > display.getHeight()) { 3 Fragment1 fragment1 = new Fragment1(); 4 getFragmentManager().beginTransaction().replace(R.id.main_layout, fragment1).commit(); 5 } else { 6 Fragment2 fragment2 = new Fragment2(); 7 getFragmentManager().beginTransaction().replace(R.id.main_layout, fragment2).commit(); 8 }
时间: 2024-10-25 23:39:02