亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

1-AVI--Fragment基礎使用

標簽:
Html/CSS

零、前言

[1].Fragment静态使用
[2].Fragment动态使用

一、Fragment静态使用

webp

静态fragment.jpg

1.蓝色布局:blue.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/colorPrimary"
                android:orientation="vertical" >    <TextView
        android:id="@+id/blue_tv"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="蓝色Fragment"
        /></RelativeLayout>
2.黄色布局:yellow.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
                android:background="@color/yellow"
              android:orientation="vertical" >    <TextView
        android:id="@+id/yellow_tv"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="黄色Fragment"
        /></RelativeLayout>
3.黄色Fragment:YellowFragment.java
/**
 * 作者:张风捷特烈<br/>
 * 时间:2018/8/28 0028:13:07<br/>
 * 邮箱:[email protected]<br/>
 * 说明:黄色Fragment
 */public class YellowFragment extends Fragment {    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {        return inflater.inflate(R.layout.yellow, container, false);
    }
}
4.绿色Fragment:BlueFragment.java
/**
 * 作者:张风捷特烈<br/>
 * 时间:2018/8/28 0028:13:07<br/>
 * 邮箱:[email protected]<br/>
 * 说明:绿色Fragment
 */public class BlueFragment extends Fragment {    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {        
        return inflater.inflate(R.layout.fragment_bottom, container, false);
    }
}
5.主布局:layout/activity_static.xml
<?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:orientation="vertical"
    tools:context=".activity.ActFragmentActivity">

    <fragment
        android:id="@+id/yellow_fragment1"

        android:name="com.toly1994.avi_fragment.staticFg.YellowFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <View
        android:background="@color/gray_8f"
        android:layout_width="match_parent"
        android:layout_height="1dp"/>

    <fragment
        android:id="@+id/yellow_fragment2"

        android:name="com.toly1994.avi_fragment.staticFg.YellowFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <fragment
        android:id="@+id/blue_fragment"
        android:name="com.toly1994.avi_fragment.staticFg.BlueFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="4"/></LinearLayout>
6.使用:StaticFragmentActivity.java
public class StaticFragmentActivity extends AppCompatActivity {    @Override
    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_static);
    }
}

二、动态使用

public class ActFragmentActivity extends FragmentActivity {    @Override
    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_acti);
        initFragment();
    }    /**
     * 动态加载Fragment
     */
    private void initFragment() {
        FragmentManager fm = getFragmentManager();//1.获取FragmentManager
        FragmentTransaction ft = fm.beginTransaction();//2.fm开启事务
        //3.动态添加 (控件id,fragment对象)
        ft.add(R.id.fl_title, new YellowFragment());
        ft.add(R.id.fl_content, new BlueFragment());
        ft.commit();//4.提交事务
    }
}

webp

动态使用fragment.png



作者:张风捷特烈
链接:https://www.jianshu.com/p/a50f196ac059


點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消