鱼C论坛

 找回密码
 立即注册
查看: 3528|回复: 0

[学习笔记] android 编程4.2.1

[复制链接]
发表于 2017-10-2 10:24:13 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 alltolove 于 2017-10-2 10:25 编辑

学习碎片必须要先安装个平板电脑的模拟器,就在genmotion开始画面新建一个tablet就行了,如图: asdassdas.png
重新建个项目,新建两个layout文件,left_fragment.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical" android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:background="@color/colorPrimary">
  6. <Button
  7.     android:layout_width="wrap_content"
  8.     android:layout_height="wrap_content"
  9.     android:id="@+id/button"
  10.     android:layout_gravity="center_horizontal"
  11.     android:text="button"/>
  12. </LinearLayout>
复制代码


right_fragment.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical" android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:background="#00ff00">
  6. <TextView
  7.     android:layout_width="wrap_content"
  8.     android:layout_height="wrap_content"
  9.     android:id="@+id/text_view"
  10.     android:textSize="20sp"
  11.     android:layout_gravity="center_horizontal"
  12.     android:text="This is right fragment"/>
  13. </LinearLayout>
复制代码

在主文件mainactivity.java边上新建两个java文件,LeftFragment.java
  1. package com.example.xinwei.fragmentactivity;

  2. import android.os.Bundle;
  3. import android.support.annotation.Nullable;
  4. import android.support.v4.app.Fragment;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;

  8. /**
  9. * Created by xinwei on 2017/10/2.
  10. */

  11. public class LeftFragment extends Fragment {
  12.     @Nullable
  13.     @Override
  14.     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  15.         View view = inflater.inflate(R.layout.left_fragment,container,false);
  16.         return view;
  17.     }
  18. }
复制代码

RightFragment.java
  1. package com.example.xinwei.fragmentactivity;

  2. import android.os.Bundle;
  3. import android.support.annotation.Nullable;
  4. import android.support.v4.app.Fragment;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;

  8. /**
  9. * Created by xinwei on 2017/10/2.
  10. */

  11. public class RightFragment extends Fragment {
  12.     @Nullable
  13.     @Override
  14.     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  15.         View view = inflater.inflate(R.layout.right_fragment,container,false);
  16.         return view;
  17.     }
  18. }
复制代码

修改activity_main.xml文件
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="horizontal" android:layout_width="match_parent"
  4.     android:layout_height="match_parent">

  5.     <fragment
  6.         android:layout_width="0dp"
  7.         android:layout_height="match_parent"
  8.         android:layout_weight="1"
  9.         android:id="@+id/left_fragment"
  10.         android:name="com.example.xinwei.fragmentactivity.LeftFragment" />

  11.     <fragment
  12.         android:layout_width="0dp"
  13.         android:layout_height="match_parent"
  14.         android:layout_weight="1"
  15.         android:id="@+id/right_fragment"
  16.         android:name="com.example.xinwei.fragmentactivity.RightFragment" />
  17. </LinearLayout>
复制代码

效果图为: saassdas.png

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-19 21:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表