|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 alltolove 于 2017-10-2 10:25 编辑
学习碎片必须要先安装个平板电脑的模拟器,就在genmotion开始画面新建一个tablet就行了,如图:
重新建个项目,新建两个layout文件,left_fragment.xml- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@color/colorPrimary">
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/button"
- android:layout_gravity="center_horizontal"
- android:text="button"/>
- </LinearLayout>
复制代码
right_fragment.xml- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#00ff00">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/text_view"
- android:textSize="20sp"
- android:layout_gravity="center_horizontal"
- android:text="This is right fragment"/>
- </LinearLayout>
复制代码
在主文件mainactivity.java边上新建两个java文件,LeftFragment.java- package com.example.xinwei.fragmentactivity;
- import android.os.Bundle;
- import android.support.annotation.Nullable;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- /**
- * Created by xinwei on 2017/10/2.
- */
- public class LeftFragment extends Fragment {
- @Nullable
- @Override
- public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.left_fragment,container,false);
- return view;
- }
- }
复制代码
RightFragment.java- package com.example.xinwei.fragmentactivity;
- import android.os.Bundle;
- import android.support.annotation.Nullable;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- /**
- * Created by xinwei on 2017/10/2.
- */
- public class RightFragment extends Fragment {
- @Nullable
- @Override
- public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.right_fragment,container,false);
- return view;
- }
- }
复制代码
修改activity_main.xml文件- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal" android:layout_width="match_parent"
- android:layout_height="match_parent">
- <fragment
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:id="@+id/left_fragment"
- android:name="com.example.xinwei.fragmentactivity.LeftFragment" />
- <fragment
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:id="@+id/right_fragment"
- android:name="com.example.xinwei.fragmentactivity.RightFragment" />
- </LinearLayout>
复制代码
效果图为:
|
|