关于这Fragment

1 | <br> |


<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/second_fragment" android:orientation="vertical" android:paddingLeft="16dp" android:paddingRight="16dp" android:paddingTop="16dp" tools:context="com.studio.app2.SecondActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用来显示Fragment的页面" /> <fragment android:name="com.studio.app2.FragmentTest" android:layout_width="wrap_content" android:layout_height="wrap_content" tools:layout="@layout/fragment_test" /> </LinearLayout>
package com.studio.app2;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Administrator on 2017/11/14.
*/
public class FragmentTest extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_test,container,false);
}
}按照视频讲解的一样写了出来,不添加fragment 到显示页面的XML中的时候就可以正常运行打开出页面,一加上箭头指的fragment到XML去 就软件出错,异常如图,看不懂,咋回事啊老师
20
收起
正在回答
1回答
静态方式实现Fragment:
1、在你的com.studio.app2包下创建继承Fragment的自定义FragmentTest类,并重写onCreateView()方法,设置布局文件(tools:layout是为了能让fragment在预览视图中正常显示);
2、在xml布局中添加fragment标签,并设置id属性或者给当前XML文件的根布局添加id(注意这里如果不设置id就会报错)
Android零基础入门2018版
- 参与学习 人
- 提交作业 5461 份
- 解答问题 7235 个
此次推出的专题为Android攻城狮培养计划的第一部分语法与界面基础篇,将带大家从0开始学习Android开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星