activity_main.xml
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:gravity="center" androID:orIEntation="horizontal" > <!-- 静态加载fragment:把fragment当成一个ui标签使用 --> <fragment androID:ID="@+ID/first" androID:name="com.gatsby.fragmentone.FirstFragment" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_weight="1" /> <fragment androID:ID="@+ID/second" androID:name="com.gatsby.fragmentone.SecondFragment" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_weight="1" /></linearLayout>
fragment_first.xml
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" > <TextVIEw androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:text="FirstFragment" androID:textSize="64sp" androID:gravity="center"/> <ImageVIEw androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:src="@drawable/image1" /></linearLayout>
fragment_second.xml
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" > <TextVIEw androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:text="SecondFragment" androID:textSize="64sp" androID:gravity="center"/> <ImageVIEw androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:src="@drawable/image8" /></linearLayout>
MaInActivity.java
package com.gatsby.fragmentone;import androID.app.Activity;import androID.os.Bundle;import androID.vIEw.Menu;import androID.vIEw.MenuItem;public class MainActivity extends Activity { @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); }}
FirstFragment.java
package com.gatsby.fragmentone;import androID.app.Fragment;import androID.os.Bundle;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;public class FirstFragment extends Fragment { @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container, Bundle savedInstanceState) { // Todo auto-generated method stub VIEw vIEw = inflater.inflate(R.layout.fragment_first, container, false); return vIEw; }}
SecondFragment.java
package com.gatsby.fragmentone;import androID.app.Fragment;import androID.os.Bundle;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;public class SecondFragment extends Fragment{ @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container, Bundle savedInstanceState) { // Todo auto-generated method stub VIEw vIEw = inflater.inflate(R.layout.fragment_second, container, false); return vIEw; }}
总结
以上是内存溢出为你收集整理的Android Fragment(一):基本使用全部内容,希望文章能够帮你解决Android Fragment(一):基本使用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)