Android 初識Fragment

1.利用Fragment完成佈局。

①:建立TOP Fragment

佈局:

<FrameLayout 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"
    tools:context="com.example.administrator.jreduch06.fragment.MainFragment">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textSize="30sp"
        android:text="MainFragment"
        android:background="#19e6d4"
        android:textColor="#ffffff"
        />
</FrameLayout>

②:建立Left Fragment


<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"
    android:background="#bece0d"
    tools:context="com.example.administrator.jreduch06.fragment.LeftFragment">

    <TextView
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:background="#dfdc24"
        android:gravity="center"
        android:textSize="30sp"
        android:textColor="#ffffff"
        android:text="LeftFragment" />
</LinearLayout>


③:建立MainFragment

<FrameLayout 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"
    tools:context="com.example.administrator.jreduch06.fragment.MainFragment">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textSize="30sp"
        android:text="MainFragment"
        android:background="#19e6d4"
        android:textColor="#ffffff"
        />
</FrameLayout>

④:建立整體佈局,將其放到一起。

<?xml version="1.0" encoding="utf-8"?>
<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"

    tools:context=".Fragment1Activity">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:name="com.example.administrator.jreduch06.fragment.TopFragment"
        android:id="@+id/top_fragment"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true">
    </fragment>
    <fragment
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:id="@+id/leftfragment"
        android:name="com.example.administrator.jreduch06.fragment.LeftFragment"
        android:layout_below="@+id/top_fragment"
        android:layout_alignParentStart="true">
    </fragment>
    <fragment
        android:layout_width="400dp"
        android:layout_height="match_parent"
        android:id="@+id/mainfragment"
        android:name="com.example.administrator.jreduch06.fragment.MainFragment"
        android:layout_below="@+id/leftfragment"
        android:layout_alignParentStart="true">
    </fragment>
</RelativeLayout>

效果:



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章