Android入门——UI(7)——Fragment

先上fragment静态加载的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:text="这是一个Fragment"
        android:background="#0f0"/>
</LinearLayout>
fragment_index.xml
package com.ouc.wkp.ui1;

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 wkp on 2016/8/25.
 */
public class FragmentIndex extends Fragment {//app包下3.0以后才可以使用

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_index,container);
        return view;
    }
}
FragmentIndex.java

注意继承的Fragment最好是

import android.support.v4.app.Fragment;  这样可以兼容早期的安卓api

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/fragment1"
        android:name="com.ouc.wkp.ui1.FragmentIndex"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
fragment_demo.xml

注意fragment元素需要指定id

package com.ouc.wkp.ui1;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;

/**
 * Created by wkp on 2016/8/25.
 */
public class FragmentDemo extends FragmentActivity{
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_demo);
    }
}
FragmentDemo.java

 

 

然后是动态加载

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent" android:background="#f00">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是一个Fragment,通过动态方式加载"
        />
</LinearLayout>
fragment_index.xml
package com.ouc.wkp.ui1;

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 wkp on 2016/8/25.
 */
public class FragmentIndex extends Fragment {//app包下3.0以后才可以使用

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //别遗忘第三个false参数,否则会出现 java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
        View view=inflater.inflate(R.layout.fragment_index,container,false);
        return view;
    }
}
FragmentIndex.java

注意代码中的那个注释

<?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">

    <Button
        android:id="@+id/btn_addd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="增加" />

    <Button
        android:id="@+id/btn_deletee"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="减少" />

    <FrameLayout
        android:id="@+id/frame_layout"
        android:layout_width="match_parent"
        android:layout_height="200dp"></FrameLayout>
</LinearLayout>
fragment_demo.xml
package com.ouc.wkp.ui1;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

/**
 * Created by wkp on 2016/8/25.
 */
public class FragmentDemo extends FragmentActivity{

    FragmentIndex fragmentIndex;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_demo);

        fragmentIndex=new FragmentIndex();

        findViewById(R.id.btn_addd).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentManager fm=getSupportFragmentManager();
                FragmentTransaction ft=fm.beginTransaction();

                ft.replace(R.id.frame_layout,fragmentIndex);
                ft.commit();
            }
        });

        findViewById(R.id.btn_deletee).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentManager fm=getSupportFragmentManager();
                FragmentTransaction ft=fm.beginTransaction();

                ft.remove(fragmentIndex);
                ft.commit();
            }
        });
    }
}
FragmentDemo.java

效果图 点击增加出现红块 点击减少消失

 

转载于:https://www.cnblogs.com/wangkaipeng/p/5805239.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值