android 之fragment创建
1.使用xml标签
1.1定义两个重要属性
<fragment
android:id="@+id/fregment_top"
android:layout_width="match_parent"
android:layout_height="220dp"
android:layout_marginTop="80dp"
android:name="com.lzh.fragment_study.Framgment_first"
/>
1.2实现一个继承自Fragment的新类
public class Framgment_first extends Fragment {
public Framgment_first() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
1.3创建一个Fragment的layout文件 fragment_main
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.lzh.fragment_study.MainActivity$PlaceholderFragment" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
/>
</RelativeLayout>
2.通过JAVA代码实现
//在程序中加入Fragment
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.container, fragment);
fragmentTransaction.commit();
getSupportFragmentManager().beginTransaction()
.add(R.id.container,new Framgment_first()).commit();
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。