fragment静态加载

 



import
android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.util.Log; /* * 测试使用Fragment(静态加载) * 1. 定义Fragment的子类, 并加载一个布局文件 * 2. 在布局文件中通过<fragment>指定指定自定义Fragment * 3. 我们的Activity必须继承于FragmentActivity * 每个Fragment本质上都会生成一个FrameLayout, 它加载的布局为其子布局 */ public class MainActivity extends FragmentActivity { public MainActivity() { Log.e("TAG", "MainActivity().."); } @Override protected void onCreate(Bundle savedInstanceState) { Log.e("TAG", "MainActivity onCreate().."); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
public class MyFragment1 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        
        //加载布局得到View对象并返回
        
        //创建一个视图对象, 设置数据并返回
        TextView  textView = new TextView(getActivity());
        textView.setText("fragment11111");
        textView.setBackgroundColor(Color.RED);
        
        return textView;
    }
}

public class MyFragment2 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        
        //加载布局得到View对象并返回
        
        //创建一个视图对象, 设置数据并返回
        TextView  textView = new TextView(getActivity());
        textView.setText("fragment22222");
        
        return textView;
    }
}
activity_main.xml
<
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> <fragment android:name="com.example.l12_fragment1.MyFragment1" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" /> <fragment android:name="com.example.l12_fragment1.MyFragment2" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" /> </LinearLayout>

 

转载于:https://www.cnblogs.com/znsongshu/p/9369906.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值