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>
  • 相关阅读:
    福建省队集训被虐记——DAY1
    bzoj1755 [Usaco2005 qua]Bank Interest
    bzoj1754 [Usaco2005 qua]Bull Math
    bzoj1753 [Usaco2005 qua]Who's in the Middle
    wikioi1369 xth 砍树
    wikioi1191 数轴染色
    bzoj1189 [HNOI2007]紧急疏散evacuate
    POJ 3734 Blocks(矩阵快速幂+矩阵递推式)
    斐波那契+大数相加
    矩阵的快速幂
  • 原文地址:https://www.cnblogs.com/fortitude/p/4978032.html
Copyright © 2011-2022 走看看