zoukankan      html  css  js  c++  java
  • Fragment用法

    Fragment,即碎片。是3.0以后推出的组件。能够让UI布局更加灵活。比如有时候,屏幕大小差别很大的设备,我们可以只用一套布局,然后控制其中一个Fragment是否显示。现在大部分UI布局都提倡Activity只控制Fragment,Fragment再来控制里面七七八八的组件。

    关于Fragment的详细讲解,郭霖大神已写出。

    http://blog.csdn.net/guolin_blog/article/details/8881711

    在此我只做一个简单的自用笔记。

    xml文件里,它是这样的:

       <FrameLayout
           android:id="@+id/fragment_content"
           android:layout_width="match_parent"
           android:layout_height="0dp"
           android:layout_weight="1">
           </FrameLayout>

    Java里,它有以下几个步骤:

    1、建一个类用来控制Fragment的显示。这个类必须继承Fragment

    public class ListFragment extends Fragment

    2、为这个Fragment用xml写一个布局,本例这个xml就叫tomato_list_layout

    3、将布局引入

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            //引入ListFragment的布局文件
            View listLayout = inflater.inflate(R.layout.tomato_list_layout,container,false);
            return listLayout;
        }

    4、获得这个类的对象

        public static ListFragment fragment1;
            fragment1 = new ListFragment();

    5、为该Fragment填充内容

            getFragmentManager().beginTransaction().replace(R.id.fragment_content,fragment1).commit();

    先这样萌萌哒告一段落吧~

  • 相关阅读:
    项目打包文件build.xml
    【转】常见面试之机器学习算法思想简单梳理
    【转】11位机器学习大牛最爱算法全解
    Simplify Path
    Text Justification
    Valid Number
    Substring with Concatenation of All Words
    Shortest Palindrome
    Palindrome Pairs
    Decode Ways
  • 原文地址:https://www.cnblogs.com/fishbone-lsy/p/4352194.html
Copyright © 2011-2022 走看看