Android 初识Fragment

深藏阁楼爱情的钟 2022-07-19 02:12 326阅读 0赞

1.利用Fragment完成布局。

①:建立TOP Fragment

布局:

  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context="com.example.administrator.jreduch06.fragment.MainFragment">
  6. <TextView
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent"
  9. android:gravity="center"
  10. android:textSize="30sp"
  11. android:text="MainFragment"
  12. android:background="#19e6d4"
  13. android:textColor="#ffffff"
  14. />
  15. </FrameLayout>

②:建立Left Fragment

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. android:background="#bece0d"
  7. tools:context="com.example.administrator.jreduch06.fragment.LeftFragment">
  8. <TextView
  9. android:layout_width="100dp"
  10. android:layout_height="match_parent"
  11. android:background="#dfdc24"
  12. android:gravity="center"
  13. android:textSize="30sp"
  14. android:textColor="#ffffff"
  15. android:text="LeftFragment" />
  16. </LinearLayout>

③:建立MainFragment

  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context="com.example.administrator.jreduch06.fragment.MainFragment">
  6. <TextView
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent"
  9. android:gravity="center"
  10. android:textSize="30sp"
  11. android:text="MainFragment"
  12. android:background="#19e6d4"
  13. android:textColor="#ffffff"
  14. />
  15. </FrameLayout>

④:建立整体布局,将其放到一起。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context=".Fragment1Activity">
  7. <fragment
  8. android:layout_width="match_parent"
  9. android:layout_height="100dp"
  10. android:name="com.example.administrator.jreduch06.fragment.TopFragment"
  11. android:id="@+id/top_fragment"
  12. android:layout_alignParentTop="true"
  13. android:layout_alignParentEnd="true">
  14. </fragment>
  15. <fragment
  16. android:layout_width="match_parent"
  17. android:layout_height="300dp"
  18. android:id="@+id/leftfragment"
  19. android:name="com.example.administrator.jreduch06.fragment.LeftFragment"
  20. android:layout_below="@+id/top_fragment"
  21. android:layout_alignParentStart="true">
  22. </fragment>
  23. <fragment
  24. android:layout_width="400dp"
  25. android:layout_height="match_parent"
  26. android:id="@+id/mainfragment"
  27. android:name="com.example.administrator.jreduch06.fragment.MainFragment"
  28. android:layout_below="@+id/leftfragment"
  29. android:layout_alignParentStart="true">
  30. </fragment>
  31. </RelativeLayout>

效果:

Center

发表评论

表情:
评论列表 (有 0 条评论,326人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Android (一)

    一、认识 Android Android 是谷歌为手机和平板电脑等其他移动设备开发的操作系统和编程平台。与 Java 类似,Android 同样也提供了软件开发工具包—S