日拱一卒(六)

1.如何动态加载Fragment

  • 在Activity的布局文件中添加存放fragment的ViewGroup:
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:layout_width="fill_parent"  
    4.     android:layout_height="fill_parent"  
    5.     android:orientation="horizontal" >  
    6.   
    7. </LinearLayout>
  • 生成Fragment的布局:
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:layout_width="fill_parent"  
    4.     android:layout_height="fill_parent"  
    5.     android:background="#00FF00"  
    6.     android:orientation="vertical" >  
    7.   
    8.     <TextView  
    9.         android:id="@+id/lblFragment1"  
    10.         android:layout_width="fill_parent"  
    11.         android:layout_height="wrap_content"  
    12.         android:text="This is fragment #1"  
    13.         android:textColor="#000000"  
    14.         android:textSize="25sp" />  
    15.   
    16. </LinearLayout>  
    Fragment1 类:
    1. public class Fragment1 extends Fragment {  
    2.     @Override  
    3.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
    4.             Bundle savedInstanceState) {  
    5.         // ---Inflate the layout for this fragment---  
    6.         return inflater.inflate(R.layout.fragment1, container, false);  
    7.     }  
    8. }  


  • 在Activity中动态添加:
    1. public class FragmentsActivity extends Activity {  
    2.     /** Called when the activity is first created. */  
    3.     @Override  
    4.     public void onCreate(Bundle savedInstanceState) {  
    5.         super.onCreate(savedInstanceState);  
    6.         setContentView(R.layout.main);  
    7.   
    8.         FragmentManager fragmentManager = getFragmentManager();  
    9.         FragmentTransaction fragmentTransaction = fragmentManager  
    10.                 .beginTransaction();  
    11.   
    12.         // ---get the current display info---  
    13.         WindowManager wm = getWindowManager();  
    14.         Display d = wm.getDefaultDisplay();  
    15.         // ---landscape mode---  
    16.         Fragment1 fragment1 = new Fragment1();  
    17.         // android.R.id.content refers to the content  
    18.         // view of the activity  
    19.        fragmentTransaction.replace(android.R.id.content, fragment1); 
    20.         // ---add to the back stack---  
    21.         fragmentTransaction.addToBackStack(null);  
    22.         fragmentTransaction.commit();  
    23.   
    24.     }  
    25.   
    26. }  

2.fragment与Activity,fragment与fragment之间如何通信?

调用FragmentManager的findFragmentById()方法,可以在活动中得到相应碎片的实例,然后就能轻松地调用碎片里的方法了。

  1. Activty调用Fragment:
    1. RightFragment rightFragment = (RightFragment) getFragmentManager().findFragmentById(R.id.right_fragment);

  2. Fragement调用Activity
    1. MainActivity activity = (MainActivity) getActivity();

  3. Fragment与Fragment:通过(1)(2)

3.如何同一个activity在平板和手机显示不同的布局?

使用限定符:res目录下新建layout-large文件夹,在这个文件夹放双页模式(程序会在左侧的面板上显示一个包含子项的列表,在右侧的面板上显示内容)的布局. Android 中一些常见的限定符可以参考下表。


屏幕特征

限定符

描述

大小

small

提供给小屏幕设备的资源

normal

提供给中等屏幕设备的资源

large

提供给大屏幕设备的资源

xlarge

提供给超大屏幕设备的资源

分辨率

ldpi

提供给低分辨率设备的资源(120dpi以下)

mdpi

提供给中等分辨率设备的资源(120dpi160dpi

hdpi

提供给高分辨率设备的资源(160dpi240dpi

xhdpi

提供给超高分辨率设备的资源(240dpi320dpi

方向

land

提供给横屏设备的资源

port

提供给竖屏设备的资源


使用最小宽度限定符:小宽度限定符允许我们对屏幕的宽度指定一个最小指(以dp为单位),然后以这个最小值为临界点,屏幕宽度大于这个值的设备就加载一个布局,屏幕宽度小于这个值的设备就加载另一个布局。如:res目录下新建layout-sw600dp文件夹,则当程序运行在屏幕宽度大于600dp的设备上时,会加载layout-sw600dp/ activity_main布局,当程序运行在屏幕宽度小于600dp的设备上时,则仍然加载默认的layout/activity_main布局。




你可能感兴趣的:(android)