Android 初识Fragment

来源:互联网 发布:含金量高的证书 知乎 编辑:程序博客网 时间:2024/06/01 23:28

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>

效果:



0 0
原创粉丝点击