zoukankan      html  css  js  c++  java
  • Android笔记: fragment简单例子

    MainActivity.java

    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            FragmentManager fm = getFragmentManager();
            Fragment fragment = fm.findFragmentById(R.id.container);
            fragment = new MyFragment();
            fm.beginTransaction().add(R.id.container, fragment).commit();
        }
    }

    MyFragment.java

    public class MyFragment extends Fragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup parent,
                Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.fragment_main, parent, false);
            return v;
        }
    }

    activity_main.xml

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    fragment_main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello,fragment!" />
    
    </RelativeLayout>
  • 相关阅读:
    第十二章,结束练习
    第十一章,表单
    第十章,表格练习
    第九章,跨行跨列的表格
    第八章,表格
    第七章,列表
    第六章,body当中的属性
    第五章,标签的使用
    6. C# 命名规则
    5. c#变量
  • 原文地址:https://www.cnblogs.com/fortitude/p/4978032.html
Copyright © 2011-2022 走看看