码迷,mamicode.com
首页 > 其他好文 > 详细

fragment在水平/垂直时的应用

时间:2015-10-21 19:13:52      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:

直接看代码

 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         } 

 

fragment在水平/垂直时的应用

标签:

原文地址:http://www.cnblogs.com/turtle920/p/4898369.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!