Android手机平板两不误 fragment(ListView)
来源:互联网 发布:微信排队叫号系统源码 编辑:程序博客网 时间:2024/04/27 18:38
Fragment工程下
1:layout文件夹下先建立fragment_left.xml
2:layout文件夹下再建立fragment_right.xml
3:com.example.fragments包下新建FragmentLeft.java
4:com.example.fragments包下再新建FragmentRight.java
5:layout文件夹下activity_main.xml写 1- fragment_left 和 2-fragment_right
6:MainActivity.java 再加载5-activity_main.xml
代码如下:
fragment_left.xml
<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:background="#ff3300" > <TextView android:id="@+id/fragment_left" android:layout_width="match_parent" android:layout_height="match_parent" android:text="this is fragement left"/></LinearLayout>
fragment_right.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#ff0394"> <TextView android:id="@+id/fragment_right" android:layout_width="match_parent" android:layout_height="match_parent" android:text="this is fragement right"/></LinearLayout>
FragmentLeft.java
public class FragmentLeft extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_left, container); }}
这里注意继承的android.app.Fragment
而不是android.support.v4.app.Fragment
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent"><fragment android:name="com.example.fragments.FragmentLeft" android:layout_height="match_parent" android:layout_width="0dp" android:layout_weight="1"/><fragment android:name="com.example.fragments.FragmentRight" android:layout_height="match_parent" android:layout_width="0dp" android:layout_weight="1"/></LinearLayout>
注意:这里用android:name="com.example.fragments.FragmentLeft"
将两个加载fragment的java文件关联起来
MainActivity.java
package com.example.fragments;import android.app.Activity;import android.app.ActionBar;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.os.Build;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }}
结果:
onAttach方法:Fragment和Activity建立关联的时候调用。
onCreateView方法:为Fragment加载布局时调用。
onActivityCreated方法:当Activity中的onCreate方法执行完后调用。
onDestroyView方法:Fragment中的布局被移除时调用。
onDetach方法:Fragment和Activity解除关联的时候调用。
Fragment真正的强大之处在于可以动态地添加到Activity当中
1:activity_main.xml用来存放手机布局
2:MenuFragment.java(内容第四步)
3:新建layout-large文件夹下的activity_main.xml,将平板分成左右两侧,左侧是fragment右侧是FrameLayout
*3.1:表明当前的Activity想加载activity_main这个布局文件。而Android系统又会根据当前的运行环境判断程序是否运行在大屏幕设备上,如果运行在大屏幕设备上,就加载layout-large目录下的activity_main.xml,否则就默认加载layout目录下的activity_main.xml。*
4:MenuFragment.java
4.1 需要ListView/adapter/data
4.2 新建sound_fragment.xml
4.3 新建SoundFragment.java
4.4 新建display_fragment.xml
4.5 新建DisplayFragment.java
4.6 新建sound_acitivity.xml和SoundActivity.java
4.7新建display_acitivity.xml和DisplayActivity.java
AndroidManifest.xml
<activity android:name="com.example.fragment.DisplayActivity"></activity><activity android:name="com.example.fragment.SoundActivity"></activity>
出错:java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child’s parent first
解决方案: View view=inflater.inflate(R.layout.display_fragment, container,**false**);
结果:
源码分享:
这里写链接内容
- Android手机平板两不误 fragment(ListView)
- Fragment(2)Android手机平板两不误,使用Fragment实现兼容手机和平板的程序
- Fragment(二)Android手机平板两不误,使用Fragment实现兼容手机和平板的程序
- Android手机平板两不误,使用Fragment实现兼容手机和平板的程序
- Android手机平板两不误,使用Fragment实现兼容手机和平板的程序
- Android手机平板两不误,使用Fragment实现兼容手机和平板的程序
- Android手机平板两不误,使用Fragment实现兼容手机和平板的程序
- Android手机平板两不误,使用Fragment实现兼容手机和平板的程序
- Android手机平板两不误,使用Fragment实现兼容手机和平板的程序
- Android手机平板两不误,使用Fragment实现兼容手机和平板的程序
- Android手机平板两不误,使用Fragment实现兼容手机和平板的程序
- Android手机平板两不误,使用Fragment实现兼容手机和平板的程序
- Android手机平板两不误,使用Fragment实现兼容手机和平板的程序
- Android手机平板两不误,使用Fragment实现兼容手机和平板的程序
- Android手机平板两不误,使用Fragment实现兼容手机和平板的程序(二)
- Android手机平板两不误,使用Fragment实现兼容手机和平板的程序
- Android手机平板两不误,使用Fragment实现兼容手机和平板的程序
- Android手机平板两不误,使用Fragment实现兼容手机和平板的程序
- WebView显示网页
- Windows Server2012 虚拟机复制及故障转移(Part1)
- Mac OS X系统上使用MacDown
- Direct-X学习笔记--变换
- PNG文件结构分析
- Android手机平板两不误 fragment(ListView)
- iOS——iPhone屏幕尺寸、分辨率及适配
- Vmware vSphere 5.0系列教程之一 Vmware vSphere 5.0简介
- 回收站删除了怎么恢复
- 我又回来啦
- SAT数学:如何读懂图表题
- UIImage图片压缩和截取
- Android组建5:android中一些常见的类型转换ID/Drawable/Byte/Bitmap..
- Java算法题