Fragment主要用于兼顾平板和手机的UI显示设计,还可用于进行布局的碎片化设计,实现解耦,使布局更清晰。
Fragment得依附于Activity存在。
举例:
编写 left_fragment.xml 布局文件
<LineaerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Button"/>
</LineaerLayout>
编写right_fragment布局文件
<LineaerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#00ff00"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:text="右Fragment"/>
</LineaerLayout>
编写相应的Fragment,继承androidx中的Fragment,重写onCreateView方法,初始化布局信息。
举例:
LeftFragment
public class LeftFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.left_fragment, container, false);
return view;
}
}
RightFragment
public class RightFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater,inflate(R.layout.right_fragment, container, false);
return view;
}
}
在布局文件中使用fragment标签,并设置属性name为相应fragment的全限定路径名。
举例:
在MainActivity中引用fragment
<LineaerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--name的值为fragment的全限定路径名-->
<fragment
android:id="@+id/left_fragment"
android:name="com.example.fragmenttest.LeftFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<fragment
android:id="@+id/right_fragment"
android:name="com.example.fragmenttest.RightFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LineaerLayout>
特性:之前的fragment都是直接在布局文件中引入的,而动态Fragment则是在java代码中,将fragment添加到布局中。
使用方式:
举例:
<LineaerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/left_fragment"
android:name="com.example.fragmenttest.LeftFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<!--将被用于替代为Fragment的布局-->
<FrameLayout
android:id="@+id/right_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LineaerLayout>
//假定添加的是RightFragment
RightFragment rightFragment = new RightFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.right_layout, fragment);
transaction.addToBackStack(null);//该方法是将fragment添加到返回栈中,参数用于描述返回栈的状态,一般传入null
transaction.commit();
运行状态:fragment是可见的,且其关联的活动处于栈顶。
暂停状态:当fragment其所依附的activity处于暂停状态时,fragment也会进入暂停状态。
停止状态:当fragment其所依附的activity处于停止状态时,fragment也会进入停止状态。或者在动态添加fragment中,创建fragment在提交事务之前调用了addToBackStack(),之后调用FragmentTransaction的remove()、replace()方法将fragment移除,此fragment会进入停止状态。
销毁状态:当fragment其所依附的activity处于销毁状态时,fragment也会进入销毁状态。或者在动态添加fragment中,创建fragment在提交事务之前没有调用addToBackStack(),之后调用FragmentTransaction的remove()、replace()方法将fragment移除,此fragment会进入销毁状态。
onAttach():当fragment和activity建立关联的时候调用。
onCreateView():当fragment创建视图时调用。
onActivityCreated():确保与fragment关联的activity一定已经创建完毕的时候调用。
onDestroyView():当与fragment关联的视图被移除的时候调用。
onDetach():当fragment和activity解除关联的时候调用。
屏幕特征 | 限定符 | 描述 |
---|---|---|
大小 | small | 提供给小屏幕设备的资源 |
normal | 提供给中等屏幕设备的资源 | |
large | 提供给大屏幕设备的资源 | |
xlarge | 提供给超大屏幕设备的资源 | |
分辨率 | ldpi | 提供给低分辨率设备的资源(120dpi以下) |
mdpi | 提供给中等分辨率设备的资源(120dpi~160dpi) | |
hdpi | 提供给高分辨率设备的资源(160dpi~240dpi) | |
xhdpi | 提供给超高分辨率设备的资源(240dpi~320dpi) | |
xxhdpi | 提供给超高高分辨率设备的资源(320dpi~48-dpi) | |
方向 | land | 提供给横屏设备的资源 |
port | 提供给竖屏设备的资源 |
原文:https://www.cnblogs.com/JoyGin/p/14188505.html