Android Fragment

首先,我先安利一波第一行代码这本书,实在是太强了,东西很全,唯一的缺点就是用kotlin语言写的,不过相比于内容,这些东西都是小问题,kotlin也挺好学的

这东西类似于HTML的frame,都是将一个页面嵌套到另一个页面中

嵌套与被嵌套的页面可以是任意一种布局,但在被嵌套的页面中的class文件,必须要继承fragment类,才可以成为fragment布局,才可以被fragment布局加载

public class Left_Fragment extends Fragment{
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_left__fragment,container,false);
        return view;
        //这个方法必须重写为这种格式,让fragement布局动态加载进来
    }
}

xml内fragment控件的样子

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <fragment
        android:id="@+id/fragment5"
        android:name="com.example.fragmenttest.Left_Fragment"
        android:layout_width="828dp"
        android:layout_height="match_parent" />

    <fragment
        android:id="@+id/fragment6"
        android:name="com.example.fragmenttest.Right_Frament"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1" />
</LinearLayout>

 一般来说写完以后会报这个错

这个错误其实无关紧要,翻译过来是这样的

<fragment>标签允许布局文件在运行时动态包含不同的布局。在布局编辑时,所使用的特定布局是未知的。在编辑布局时,您可以选择您想要预览的布局。

他的意思是,你如果想在这个界面预览fragement弄完以后的效果,就点后面的PickLayout选择一个布局让他显示出来即可

动态添加Fragement

首先,被嵌入的fragment子布局控件可以在父布局内的class直接findviewByid找到,并不用在子布局的class内写

然后

你得有一个frameLayout,这个作为你动态改变的fragment的容器来使用,就像这样

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <fragment
        android:id="@+id/fragment5"
        android:name="com.example.fragmenttest.Left_Fragment"
        android:layout_width="828dp"
        android:layout_height="match_parent" />

    <FrameLayout
        android:id="@+id/fragmentLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </FrameLayout>

</LinearLayout>

有了这些以后就可以写监听器来动态添加fragment了

btn = findViewById(R.id.button);//先虚空获取一下button这个控件,虽然代码提示没有,但是可以获取到
btn2 = findViewById(R.id.button2);
btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        FragmentManager fragment = getSupportFragmentManager();
        FragmentTransaction transaction = fragment.beginTransaction();
        transaction.replace(R.id.fragmentLayout, new activity_right2_fragment());
        transaction.commit();
    }
});
FragmentManager fragment = getSupportFragmentManager();
//获取fragmentManager
FragmentTransaction transaction = fragment.beginTransaction();
//开启一个事务
transaction.replace(R.id.fragmentLayout, new activity_right2_fragment());
//向容器内添加或替换fragment,第一个参数为frameLayout(布局)的id,第二个参数为new一个要替换的fragment布局的类
transaction.commit();
//提交事务,不写这玩意,点了以后没反应

至此,就可以点击按钮改变fragment了

返回栈(FragmentTransaction.addToBackStack())

指按下返回键以后,退回到上一个fragment,这个东西是给容器设置的

btn2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fragmentLayout,new Right_Frament());
        fragmentTransaction.addToBackStack(null);//这条语句就是用来设置返回栈的
        fragmentTransaction.commit();
    }
});

限定符

如果同一个app想在手机上显示一页内容,平板上显示两页内容,就可以用限定符

 

创建这种界面步骤

 

 

它会根据选项自动生成目录名称

File name可以随便起

也可以通过最小宽度限定符来实现这个功能

原理是小于该宽度,就使用单页布局,大于该宽度,使用多页布局,单位是DP

这个东西写出来以后,实际上还是受MainActivity控制的,

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值