碎片Fragment
2015-11-17 17:40
176 查看
fragment适用用平板电脑(大屏幕)
注意要点:
1.MainActivity要继承 FragmentActivity 而不是Activity
2.LeftFragment 继承 Fragment时要import android.support.v4.app.Fragment;而不是import android.app.Fragment;
3.在activity_main.xml里声明碎片使用fragment而不是Fragment或者android.support.v4.app.Fragment
left_fragment.xml
right_fragment.xml
LeftFragment.java
RightFragment.java
activity_main.xml
MainActivity.java
![](http://img.blog.csdn.net/20151117174704031)
让不同大小的移动设备显示不同的布局
在res/下创建layout-large或者layout-sw600dp文件夹。
layout-large是指大屏幕,但是不够灵活,所以使用layout-sw600dp(屏幕尺寸大于600dp)。
layout-sw600dp要在模拟器的platform大于4.0才有效
注意要点:
1.MainActivity要继承 FragmentActivity 而不是Activity
2.LeftFragment 继承 Fragment时要import android.support.v4.app.Fragment;而不是import android.app.Fragment;
3.在activity_main.xml里声明碎片使用fragment而不是Fragment或者android.support.v4.app.Fragment
left_fragment.xml
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#333" xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1" android:textColor="#fff"/> </LinearLayout>
right_fragment.xml
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2"/> </LinearLayout>
LeftFragment.java
package com.example.fragment; import com.example.testsometing.R; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class LeftFragment extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.left_fragment, container,false); return view; } }
RightFragment.java
package com.example.fragment; import com.example.testsometing.R; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class RightFragment extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.right_fragment, container,false); return view; } }
activity_main.xml
<LinearLayout xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" > <fragment android:id="@+id/leftfragment" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:name="com.example.fragment.LeftFragment"/> <fragment android:id="@+id/rightfragment" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:name="com.example.fragment.RightFragment"/> </LinearLayout>
MainActivity.java
public class MainActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
让不同大小的移动设备显示不同的布局
在res/下创建layout-large或者layout-sw600dp文件夹。
layout-large是指大屏幕,但是不够灵活,所以使用layout-sw600dp(屏幕尺寸大于600dp)。
layout-sw600dp要在模拟器的platform大于4.0才有效
相关文章推荐
- 应用dts在am335x上移植LCD(三)
- 图片在沙盒中的存在形式
- iOS_打包上传appStore
- 可折叠tableView - 仿照通讯录
- Linux下安装nginx和php
- 委托 Action与Func
- POJ 2049 Finding Nemo
- 使用dom4j 把xml格式解析为对象或者对象集合
- 2015 联赛 (2)
- 应用dts在am335x上移植LCD(二)
- AVAudioPlayer的简单使用
- iOS_基于ScrollView的一个简单相册
- Eclipse上GIT插件EGIT--_Rebase和Merge的区别
- 将Python脚本打包成可执行文件
- 解析#define NULL ((void *)0)——野指针,空指针和 void*
- Java使用RSA加密解密签名及校验
- javaScript cookies
- python装饰器decorator之计算函数运行时间的例子
- SSH:Spring框架(简介)
- 使用MyBatis的ScriptRunner执行sql文件