Android中利用Fragment同时支持屏幕和手机
2016-06-15 10:16
555 查看
1.设计横屏时屏幕左边的样式,定义一个类FragmentA继承自ListFragment
重写onAttach()方法,并且定义数据源。
2.在onAttach()方法中设置我们的适配器用于给FragmentA提供数据
3.新建一个FragmentB继承自Fragment,在布局文件中定义fragment_b的布局文件并在FragmentB中解析:
4.这里我们想让FragmentA来控制FragmentB的内容更改,为了降低程序的耦合性,我们设置接口
5.当竖屏的时候,我们仅显示FragmentA,所以我们在activity_main中加入FragmentA:
此时我们要显示第二个界面,所以在layout文件夹下面添加布局文件activity_selected,添加FragmentB,用于显示FragmentB:
6.此时我们的竖屏界面已经准备完毕。我们为了显示横屏呈现不同的效果,在res下新建文件夹:layout-land:然后将actvity_main复制粘贴到此文件夹下。
这时候,修改布局文件,放两个
Fragment不是放一个:
7.由于我们使用support.v4包下的Fragment所以MainActivity要继承于FragmentActivity
重写onAttach()方法,并且定义数据源。
2.在onAttach()方法中设置我们的适配器用于给FragmentA提供数据
String[] data={"计算机组成原理","操作系统原理","编译原理","算法","JAVAEE","JAVAWEB","Python", "PHP","C++"}; @Override public void onAttach(Context context) { super.onAttach(context); //由于我们继承的是ListFragment所以设置适配器的时候我们直接设置,适配器的类型是listAdatapter setListAdapter(new ArrayAdapter<String>( getActivity(), //第一个参数是上下文 android.R.layout.simple_expandable_list_item_2, //设置样式,项的样式 android.R.id.text1, //要设置数据源的项的id data )); }
3.新建一个FragmentB继承自Fragment,在布局文件中定义fragment_b的布局文件并在FragmentB中解析:
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_b,null); return view; } //找到布局文件中的控件,因此要重写以下的方法,必须调用view的findviewbyid的方法 @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); textview=(TextView) view.findViewById(R.id.textView1); }
4.这里我们想让FragmentA来控制FragmentB的内容更改,为了降低程序的耦合性,我们设置接口
public interface SelectedItemListener { public void selectedItem(String text); }
5.当竖屏的时候,我们仅显示FragmentA,所以我们在activity_main中加入FragmentA:
<fragment android:id="@+id/fragment1" android:name="com.example.androiddemo001.Fragment.FragmentA" android:layout_width="match_parent" android:layout_height="match_parent" />
此时我们要显示第二个界面,所以在layout文件夹下面添加布局文件activity_selected,添加FragmentB,用于显示FragmentB:
<fragment android:id="@+id/fragment1" android:name="com.example.androiddemo001.Fragment.FragmentB" android:layout_width="match_parent" android:layout_height="wrap_content" />
6.此时我们的竖屏界面已经准备完毕。我们为了显示横屏呈现不同的效果,在res下新建文件夹:layout-land:然后将actvity_main复制粘贴到此文件夹下。
这时候,修改布局文件,放两个
Fragment不是放一个:
<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:orientation="horizontal" tools:context="com.example.androiddemo001.MainActivity" > <LinearLayout android:layout_width="0dp" android:layout_weight="30" android:layout_height="match_parent" android:orientation="vertical" android:background="#FF0000" > <fragment android:id="@+id/fragment1" android:name="com.example.androiddemo001.Fragment.FragmentA" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_weight="70" android:layout_height="match_parent" android:orientation="vertical" android:background="#00FF00" > <fragment android:id="@+id/fragment2" android:name="com.example.androiddemo001.Fragment.FragmentB" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> </LinearLayout>
7.由于我们使用support.v4包下的Fragment所以MainActivity要继承于FragmentActivity
相关文章推荐
- Android中Fragment简记
- Android Fragment应用实战(音乐播放器界面)
- Android 获取View宽度
- android java.lang.LinkageError解决,版本兼容
- Android获取系统高度、标题栏和状态栏高度
- Android Studio查找有中文字符的文件位置
- 3、Android构建仪表测试
- 3、Android构建仪表测试
- Android最全的屏幕适配
- Mac下打开win的android studio工程
- 快速开发封装精简版
- Android多线程断点续传下载
- Android Animation模式详解
- Android Studio调试功能使用总结【转】
- Android Studio使用心得
- Android_ListView 用多样式item实现复杂布局显示
- Android6.0编译时out目录下文件无法自动生成问题解决方案
- android6.15 intent flag 以及线性布局中的gravity
- Android 主题动态切换框架:Prism
- 【转】Android中removeCallbacks失效原因