{"id":306,"date":"2023-02-01T19:47:17","date_gmt":"2022-11-22T22:32:02","guid":{"rendered":"https:\/\/www.silicloud.com\/zh\/blog\/index.php\/2023\/11\/30\/android%e7%a2%8e%e7%89%87%e7%9a%84%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f\/"},"modified":"2025-08-01T20:26:50","modified_gmt":"2025-08-01T12:26:50","slug":"android%e7%a2%8e%e7%89%87%e7%9a%84%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/zh\/blog\/android%e7%a2%8e%e7%89%87%e7%9a%84%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f\/","title":{"rendered":"Android\u788e\u7247\u751f\u547d\u5468\u671f\u8be6\u89e3\uff1a\u5f00\u53d1\u5fc5\u5907\u77e5\u8bc6\u70b9\u4e0e\u6700\u4f73\u5b9e\u8df5"},"content":{"rendered":"<p>\u4eca\u5929\u6211\u4eec\u5c06\u5b66\u4e60Android\u7247\u6bb5\u751f\u547d\u5468\u671f\uff0c\u5e76\u5728Android\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5b9e\u73b0\u4e00\u4e2a\u5305\u542b\u4e24\u4e2a\u7247\u6bb5\u7684\u5355\u4e00\u6d3b\u52a8\u7c7b\u3002<\/p>\n<h3>\u5b89\u5353\u7684\u788e\u7247Android\u4e2d\u7684Fragment\u7c7b\u7528\u4e8e\u6784\u5efa\u52a8\u6001\u7528\u6237\u754c\u9762\u3002Fragment\u5e94\u8be5\u5728Activity\u4e2d\u4f7f\u7528\u3002Fragment\u7684\u6700\u5927\u4f18\u70b9\u662f\u7b80\u5316\u4e86\u4e3a\u591a\u79cd\u5c4f\u5e55\u5c3a\u5bf8\u521b\u5efa\u7528\u6237\u754c\u9762\u7684\u4efb\u52a1\u3002\u4e00\u4e2aActivity\u53ef\u4ee5\u5305\u542b\u4efb\u610f\u6570\u91cf\u7684Fragment\u3002Android\u7684Fragment\u672c\u8eab\u4e0d\u662fView\u7684\u5b50\u7c7b\uff0c\u800c\u5927\u591a\u6570\u5176\u4ed6UI\u7ec4\u4ef6\u662fView\u7684\u5b50\u7c7b\u3002\u76f8\u53cd\uff0cFragment\u5185\u90e8\u6709\u4e00\u4e2a\u89c6\u56fe\u3002\u6700\u7ec8\uff0c\u8fd9\u4e2a\u89c6\u56fe\u5c06\u5728Fragment\u6240\u5728\u7684Activity\u4e2d\u663e\u793a\u51fa\u6765\u3002\u7531\u4e8eAndroid\u7684Fragment\u4e0d\u662f\u4e00\u4e2aView\uff0c\u6240\u4ee5\u5c06\u5176\u6dfb\u52a0\u5230Activity\u4e2d\u7684\u65b9\u6cd5\u4e0e\u6dfb\u52a0View\uff08\u4f8b\u5982TextView\uff09\u6709\u6240\u4e0d\u540c\u3002Fragment\u88ab\u6dfb\u52a0\u5230Activity\u5185\u7684ViewGroup\u4e2d\u3002Fragment\u7684\u89c6\u56fe\u663e\u793a\u5728\u8fd9\u4e2aViewGroup\u5185\u3002\u4ee5\u4e0b\u56fe\u4f8b\u5c55\u793a\u4e86\u5f53Fragment\u88ab\u6dfb\u52a0\u5230Activity\u4e2d\u65f6\u53d1\u751f\u7684\u60c5\u51b5\uff1a\u9996\u5148\uff0cActivity\u83b7\u53d6\u5bf9Fragment\u7684\u5f15\u7528\u3002\u7136\u540e\uff0c\u5b83\u83b7\u53d6\u5bf9\u5c06\u6e32\u67d3Fragment\u89c6\u56fe\u7684ViewGroup\u7684\u5f15\u7528\u3002\u7136\u540eActivity\u6dfb\u52a0Fragment\u3002\u63a5\u7740\uff0cFragment\u521b\u5efa\u5b83\u7684\u89c6\u56fe\u5e76\u5c06\u5176\u8fd4\u56de\u7ed9Activity\u3002\u89c6\u56fe\u7136\u540e\u88ab\u63d2\u5165\u5230ViewGroup\u7236\u5bb9\u5668\u4e2d\uff0cFragment\u5c31\u5f00\u59cb\u8fd0\u884c\u4e86\u3002<\/p>\n<h2>\u788e\u7247\u751f\u547d\u5468\u671f\u4ee5\u4e0b\u56fe\u7247\u5c55\u793a\u4e86Android fragment\u7684\u751f\u547d\u5468\u671f\u3002\u4e0b\u9762\u662ffragment\u751f\u547d\u5468\u671f\u7684\u65b9\u6cd5\u3002<\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>onAttach() \uff1a\u6b64\u65b9\u6cd5\u5c06\u9996\u5148\u88ab\u8c03\u7528\uff0c\u751a\u81f3\u5728onCreate()\u4e4b\u524d\uff0c\u8ba9\u6211\u4eec\u77e5\u9053\u4f60\u7684fragment\u5df2\u7ecf\u4e0eactivity\u5173\u8054\u4e86\u8d77\u6765\u3002\u4f60\u4f1a\u88ab\u4f20\u9012\u4f60\u7684fragment\u6240\u5c5e\u7684activity\u3002<\/ol>\n<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>onCreateView() \uff1a\u7cfb\u7edf\u5728fragment\u7b2c\u4e00\u6b21\u7ed8\u5236\u5176UI\u65f6\u8c03\u7528\u6b64\u56de\u8c03\u65b9\u6cd5\u3002\u4e3a\u4e86\u7ed8\u5236fragment\u7684UI\uff0c\u5fc5\u987b\u4ece\u6b64\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2aView\u7ec4\u4ef6\uff0c\u8be5\u7ec4\u4ef6\u662ffragment\u5e03\u5c40\u7684\u6839\u90e8\u3002\u5982\u679cfragment\u4e0d\u63d0\u4f9bUI\uff0c\u5219\u53ef\u4ee5\u8fd4\u56denull\u3002<\/ol>\n<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>onViewCreated() \uff1a\u6b64\u65b9\u6cd5\u5c06\u5728onCreateView()\u4e4b\u540e\u88ab\u8c03\u7528\u3002\u5f53\u7ee7\u627f\u4e86onCreateView()\u7684\u5b9e\u73b0\u4f46\u9700\u8981\u914d\u7f6e\u7ed3\u679c\u89c6\u56fe\u65f6\uff0c\u8fd9\u5c06\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u4f7f\u7528ListFragment\u65f6\u8bbe\u7f6e\u9002\u914d\u5668\u3002<\/ol>\n<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>onActivityCreated() \uff1a\u6b64\u65b9\u6cd5\u5728onCreate()\u548conCreateView()\u4e4b\u540e\u88ab\u8c03\u7528\uff0c\u6307\u793aactivity\u7684onCreate()\u5df2\u5b8c\u6210\u3002\u5982\u679cfragment\u4e2d\u6709\u4e9b\u4e1c\u897f\u4f9d\u8d56\u4e8eactivity\u7684onCreate()\u5df2\u5b8c\u6210\u7684\u5de5\u4f5c\u6765\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528onActivityCreated()\u8fdb\u884c\u521d\u59cb\u5316\u5de5\u4f5c\u3002<\/ol>\n<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>onStart() \uff1a\u4e00\u65e6fragment\u53d8\u5f97\u53ef\u89c1\uff0c\u5c06\u8c03\u7528onStart()\u65b9\u6cd5\u3002<\/ol>\n<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>onPause() \uff1a\u7cfb\u7edf\u5c06\u6b64\u65b9\u6cd5\u4f5c\u4e3a\u7528\u6237\u79bb\u5f00fragment\u7684\u7b2c\u4e00\u4e2a\u8ff9\u8c61\u8c03\u7528\u3002\u901a\u5e38\uff0c\u5728\u6b64\u5904\u5e94\u63d0\u4ea4\u4efb\u4f55\u9700\u8981\u5728\u5f53\u524d\u7528\u6237\u4f1a\u8bdd\u4e4b\u5916\u6301\u4e45\u4fdd\u5b58\u7684\u66f4\u6539\u3002<\/ol>\n<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>onStop() \uff1a\u901a\u8fc7\u8c03\u7528onStop()\u65b9\u6cd5\u4f7ffragment\u505c\u6b62\u3002<\/ol>\n<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>onDestroyView() \uff1a\u5b83\u5728onDestroy()\u4e4b\u524d\u88ab\u8c03\u7528\u3002\u8fd9\u662fonCreateView()\u7684\u5bf9\u5e94\u65b9\u6cd5\uff0c\u5728\u6b64\u65b9\u6cd5\u4e2d\u6211\u4eec\u8bbe\u7f6eUI\u3002\u5982\u679c\u6709\u4e00\u4e9b\u9700\u8981\u6839\u636eUI\u7279\u5b9a\u6e05\u7406\u7684\u4e1c\u897f\uff0c\u53ef\u4ee5\u5c06\u8be5\u903b\u8f91\u653e\u5728onDestroyView()\u4e2d\u3002<\/ol>\n<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>onDestroy() \uff1a\u8c03\u7528onDestroy()\u4ee5\u5bf9fragment\u7684\u72b6\u6001\u8fdb\u884c\u6700\u7ec8\u6e05\u7406\uff0c\u4f46Android\u5e73\u53f0\u4e0d\u4fdd\u8bc1\u4f1a\u8c03\u7528\u6b64\u65b9\u6cd5\u3002<\/ol>\n<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<ol>onDetach() \uff1a\u5b83\u5728onDestroy()\u4e4b\u540e\u88ab\u8c03\u7528\uff0c\u7528\u4e8e\u901a\u77e5fragment\u4e0e\u5176\u6258\u7ba1\u7684activity\u5df2\u7ecf\u89e3\u9664\u5173\u8054\u3002<\/ol>\n<h3>Android\u7684Fragment\u7c7b\u5728Honeycomb\uff08API 11\uff09\u4e2d\uff0cAndroid API \u589e\u52a0\u4e86\u7247\u6bb5\u3002<\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>android.app.Fragment\uff1a\u6240\u6709\u7247\u6bb5\u5b9a\u4e49\u7684\u57fa\u7c7b<\/ol>\n<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>android.app.FragmentManager\uff1a\u5728\u6d3b\u52a8\u4e2d\u4e0e\u7247\u6bb5\u5bf9\u8c61\u8fdb\u884c\u4ea4\u4e92\u7684\u7c7b<\/ol>\n<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>android.app.FragmentTransaction\uff1a\u6267\u884c\u4e00\u7ec4\u7247\u6bb5\u64cd\u4f5c\u7684\u7c7b<\/ol>\n<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<ol>\u4f7f\u7528\u8c37\u6b4c\u63d0\u4f9b\u7684\u517c\u5bb9\u5305\u5e93\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u7c7b\u6765\u8fdb\u884c\u5b9e\u73b0\u3002<\/ol>\n<ul class=\"post-ul\">\n<li>android.support.v4.app.FragmentActivity : The base class for all activities using compatibility-based fragment (and loader) features<\/li>\n<li>android.support.v4.app.Fragment<\/li>\n<li>android.support.v4.app.FragmentManager<\/li>\n<li>android.support.v4.app.FragmentTransaction<\/li>\n<\/ul>\n<h3>Android Fragment\u7684onCreateView()\u65b9\u6cd5\u8fd9\u662f\u4e00\u4e2a\u4f7f\u7528onCreateView()\u6765\u5b9e\u73b0\u7684\u793a\u4f8b\u7247\u6bb5\u3002<\/p>\n<pre class=\"post-pre\"><code>public class SampleFragment extends Fragment {\r\n\r\n    @Override\r\n    public View onCreateView(LayoutInflater inflater, ViewGroup parentViewGroup,\r\n                             Bundle savedInstanceState) {\r\n\r\n        View rootView = inflater.inflate(R.layout.fragment_sample, parentViewGroup, false);\r\n        return rootView;\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>onCreateView()\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2aLayoutInflater\u3001\u4e00\u4e2aViewGroup\u548c\u4e00\u4e2aBundle\u4f5c\u4e3a\u53c2\u6570\u3002LayoutInflater\u662f\u4e00\u4e2a\u7ec4\u4ef6\uff0c\u53ef\u4ee5\u6839\u636e\u5e03\u5c40XML\u6587\u4ef6\u521b\u5efaView\u5b9e\u4f8b\u3002\u6b63\u5982\u60a8\u6240\u770b\u5230\u7684\uff0c\u793a\u4f8b\u5b9e\u9645\u4e0a\u901a\u8fc7\u8c03\u7528layout.inflate()\u6765\u6267\u884c\u6b64\u64cd\u4f5c\u3002inflate()\u65b9\u6cd5\u63a5\u53d7\u4e09\u4e2a\u53c2\u6570\uff1a\u5e03\u5c40XML\u6587\u4ef6\u7684ID\uff08\u5728R.layout\u5185\uff09\uff0c\u8981\u5c06\u7247\u6bb5\u7684View\u63d2\u5165\u5176\u4e2d\u7684\u7236ViewGroup\uff0c\u4ee5\u53ca\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u7528\u4e8e\u6307\u793a\u662f\u5426\u5c06\u4ece\u5e03\u5c40XML\u6587\u4ef6\u4e2d\u81a8\u80c0\u7684\u7247\u6bb5\u7684View\u63d2\u5165\u5230\u7236ViewGroup\u4e2d\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5c06\u4f20\u9012false\uff0c\u56e0\u4e3aView\u5c06\u901a\u8fc7\u6211\u4eec\u8c03\u7528\u7684\u4e00\u4e9bAndroid\u4ee3\u7801\u9644\u52a0\u5230\u7236ViewGroup\u4e2d\u7684\u5176\u4ed6\u4f4d\u7f6e\u3002\u5f53\u60a8\u5c06false\u4f5c\u4e3ainflate()\u7684\u6700\u540e\u4e00\u4e2a\u53c2\u6570\u4f20\u9012\u65f6\uff0c\u7236ViewGroup\u4ecd\u7136\u7528\u4e8e\u81a8\u80c0View\u7684\u5e03\u5c40\u8ba1\u7b97\uff0c\u56e0\u6b64\u4e0d\u80fd\u5c06null\u4f5c\u4e3a\u7236ViewGroup\u4f20\u9012\u3002onCreateView()\u7684ViewGroup\u53c2\u6570\u662f\u8981\u5c06\u7247\u6bb5\u7684View\u63d2\u5165\u5176\u4e2d\u7684\u7236ViewGroup\u3002\u8fd9\u662f\u6d3b\u52a8\u4e2d\u7684\u4e00\u4e2aViewGroup\uff0c\u5c06\u201c\u6258\u7ba1\u201d\u7247\u6bb5\u3002onCreateView()\u7684Bundle\u53c2\u6570\u662f\u4e00\u4e2aBundle\uff0c\u7247\u6bb5\u53ef\u4ee5\u5728\u5176\u4e2d\u4fdd\u5b58\u6570\u636e\uff0c\u5c31\u50cf\u5728Activity\u4e2d\u4e00\u6837\u3002<\/p>\n<h3>Android\u788e\u7247\u793a\u4f8bAndroid\u788e\u7247\u793a\u4f8b\u9879\u76ee\u7531\u4e00\u4e2a\u6d3b\u52a8\u7ec4\u6210\uff0c\u8be5\u6d3b\u52a8\u5206\u522b\u5305\u542b\u4e24\u4e2a\u788e\u7247\uff1aTextFragment\u548cMenuFragment\u3002<\/p>\n<h3>Android Fragment \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>MainActivity\u5305\u542b\u4e86\u4e24\u4e2a\u788e\u7247\uff1aTextFragment\u548cMenuFragment\u3002\u56e0\u6b64\uff0c\u8ba9\u6211\u4eec\u5f00\u59cb\u4f7f\u7528XML\u5e03\u5c40\u6587\u4ef6activity_main.xml\u6765\u5b9a\u4e49\u8fd9\u4e9b\u788e\u7247\u3002<\/p>\n<pre class=\"post-pre\"><code>&lt;LinearLayout xmlns:android=\"https:\/\/schemas.android.com\/apk\/res\/android\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    android:orientation=\"horizontal\"\r\n    android:weightSum=\"1.0\"&gt;\r\n\r\n    &lt;fragment\r\n        android:layout_height=\"match_parent\"\r\n        android:layout_width=\"match_parent\"\r\n        class=\"scdev.com.fragments.fragments.MenuFragment\"\r\n        android:id=\"@+id\/fragment\"\r\n        android:layout_weight=\"0.5\"\/&gt;\r\n    &lt;fragment\r\n        android:layout_width=\"match_parent\"\r\n        android:layout_height=\"match_parent\"\r\n        class=\"scdev.com.fragments.fragments.TextFragment\"\r\n        android:id=\"@+id\/fragment2\"\r\n        android:layout_weight=\"0.5\"\/&gt;\r\n&lt;\/LinearLayout&gt;\r\n<\/code><\/pre>\n<p>\u5982\u6211\u4eec\u6240\u89c1\uff0c\u4f5c\u4e3a\u8be5\u6d3b\u52a8\u7684\u4e00\u90e8\u5206\uff0c\u788e\u7247\u7684\u7c7b\u6587\u4ef6\u88ab\u5b9a\u4e49\u4e3aclass=&#8221;scdev.com.fragments.fragments.TextFragment&#8221;\u3002\u788e\u7247\u7c7b\u53ca\u5176\u5e03\u5c40\u5982\u4e0b\u4ee3\u7801\u6240\u793a\u3002<\/p>\n<pre class=\"post-pre\"><code>package scdev.com.fragments.fragments;\r\n\r\nimport android.app.Fragment;\r\nimport android.os.Bundle;\r\nimport android.view.LayoutInflater;\r\nimport android.view.View;\r\nimport android.view.ViewGroup;\r\nimport android.widget.TextView;\r\n\r\nimport scdev.com.fragments.R;\r\npublic class TextFragment extends Fragment {\r\n    TextView text,vers;\r\n\r\n    @Override\r\n\r\n    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {\r\n\r\n        View view = inflater.inflate(R.layout.text_fragment, container, false);\r\n        text= (TextView) view.findViewById(R.id.AndroidOs);\r\n        vers= (TextView)view.findViewById(R.id.Version);\r\n\r\n\r\n        return view;\r\n\r\n    }\r\n    public void change(String txt, String txt1){\r\n        text.setText(txt);\r\n        vers.setText(txt1);\r\n\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>XML\u683c\u5f0f\u7684TextFragment\u5e03\u5c40<\/p>\n<pre class=\"post-pre\"><code>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n\r\n&lt;LinearLayout xmlns:android=\"https:\/\/schemas.android.com\/apk\/res\/android\"\r\n    android:orientation=\"vertical\"\r\n    android:layout_width=\"match_parent\"\r\n    android:gravity=\"center\"\r\n    android:background=\"#5ba4e5\"\r\n    android:layout_height=\"match_parent\"&gt;\r\n\r\n    &lt;TextView\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:textSize=\"40px\"\r\n        android:textColor=\"#ffffff\"\r\n        android:layout_gravity=\"center\"\r\n        android:id=\"@+id\/AndroidOs\"\/&gt;\r\n    &lt;TextView\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_gravity=\"center\"\r\n        android:textColor=\"#ffffff\"\r\n        android:textSize=\"30px\"\r\n        android:id=\"@+id\/Version\"\/&gt;\r\n\r\n&lt;\/LinearLayout&gt;\r\n<\/code><\/pre>\n<p>TextFragment\u5305\u62ec\u4e24\u4e2aTextView\uff0c\u5206\u522b\u663e\u793aAndroid\u7248\u672c\u7684\u540d\u79f0\u548c\u7f16\u53f7\u3002<\/p>\n<pre class=\"post-pre\"><code>package scdev.com.fragments.fragments;\r\n\r\nimport android.app.ListFragment;\r\nimport android.os.Bundle;\r\nimport android.view.LayoutInflater;\r\nimport android.view.View;\r\nimport android.view.ViewGroup;\r\nimport android.widget.ArrayAdapter;\r\nimport android.widget.ListView;\r\n\r\nimport scdev.com.fragments.R;\r\npublic class MenuFragment extends ListFragment {\r\n    String[] AndroidOS = new String[] { \"Cupcake\",\"Donut\",\"Eclair\",\"Froyo\",\"Gingerbread\",\"Honeycomb\",\"Ice Cream SandWich\",\"Jelly Bean\",\"KitKat\" };\r\n    String[] Version = new String[]{\"1.5\",\"1.6\",\"2.0-2.1\",\"2.2\",\"2.3\",\"3.0-3.2\",\"4.0\",\"4.1-4.3\",\"4.4\"};\r\n    @Override\r\n\r\n    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {\r\n        View view =inflater.inflate(R.layout.list_fragment, container, false);\r\n        ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(getActivity(),\r\n                android.R.layout.simple_list_item_1, AndroidOS);\r\n        setListAdapter(adapter);\r\n\r\n        return view;\r\n\r\n    }\r\n    @Override\r\n    public void onListItemClick(ListView l, View v, int position, long id) {\r\n        TextFragment txt = (TextFragment)getFragmentManager().findFragmentById(R.id.fragment2);\r\n        txt.change(AndroidOS[position],\"\u7248\u672c\uff1a\"+Version[position]);\r\n        getListView().setSelector(android.R.color.holo_blue_dark);\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>MenuFragment\u7684XML\u5e03\u5c40<\/p>\n<pre class=\"post-pre\"><code>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n\r\n&lt;LinearLayout xmlns:android=\"https:\/\/schemas.android.com\/apk\/res\/android\"\r\n    android:orientation=\"vertical\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"&gt;\r\n    &lt;ListView\r\n        android:layout_width=\"match_parent\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:id=\"@android:id\/list\" \/&gt;\r\n&lt;\/LinearLayout&gt;\r\n<\/code><\/pre>\n<p>MenuFragment\u5c55\u793a\u4e86\u4e00\u4e2aListView\u3002\u5982\u6211\u4eec\u6240\u89c1\uff0cListView\u4f7f\u7528\u7684\u662f\u9ed8\u8ba4\u7684simple_list_item_1\u5e03\u5c40\uff0c\u8fd9\u4e0e\u6211\u4eec\u5728\u4e4b\u524d\u7684\u6587\u7ae0\u4e2d\u4e3aListView\u521b\u5efa\u7684\u81ea\u5b9a\u4e49\u5e03\u5c40\u76f8\u5bf9\u5e94\u3002MainActivity\u901a\u8fc7\u8c03\u7528onCreate\u65b9\u6cd5\u4e2d\u7684setContentView\u65b9\u6cd5\u6765\u8c03\u7528\u5b83\u3002\u788e\u7247\u662f\u4eceXML\u6587\u4ef6\u4e2d\u8c03\u7528\u7684\u3002\u53e6\u5916\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528FragmentManager\u4ece\u6d3b\u52a8\u7c7b\u4e2d\u6dfb\u52a0\u788e\u7247\uff0c\u5982\u4e0b\u9762\u7684\u4ee3\u7801\u6240\u793a\uff1a<\/p>\n<pre class=\"post-pre\"><code>getFragmentManager()\r\n                  .beginTransaction()\r\n                  .add(R.id.fragmentParentViewGroup, new MyFragment())\r\n                  .commit();\r\n<\/code><\/pre>\n<p>\u5728\u4e0b\u9762\u7684\u793a\u4f8b\u4e2d\uff0cid fragmentParentViewGroup\u5c5e\u4e8e\u4e0b\u65b9\u663e\u793a\u7684FrameLayout\u3002<\/p>\n<pre class=\"post-pre\"><code>&lt;FrameLayout xmlns:android=\"https:\/\/schemas.android.com\/apk\/res\/android\"\r\n             xmlns:tools=\"https:\/\/schemas.android.com\/tools\"\r\n             android:id=\"@+id\/fragmentParentViewGroup\"\r\n             android:layout_width=\"match_parent\"\r\n             android:layout_height=\"match_parent\"\r\n             tools:context=\".MyActivity\"\r\n             tools:ignore=\"MergeRootFrame\" \/&gt;\r\n<\/code><\/pre>\n<h3>Android\u788e\u7247\u793a\u4f8b\u5e94\u7528<\/h3>\n<p>\u4ee5\u4e0b\u663e\u793a\u4e86\u6211\u4eec\u9879\u76ee\u751f\u6210\u7684\u8f93\u51fa\uff0c\u60a8\u53ef\u4ee5\u770b\u5230\u6b64\u5904\u5b58\u5728\u4e24\u4e2a\u788e\u7247\uff0c\u5f53\u60a8\u5728\u5de6\u4fa7\u788e\u7247\u4e2d\u9009\u62e9\u4efb\u610f\u4e00\u4e2a\u65f6\uff0c\u6570\u636e\u5c06\u586b\u5145\u5230\u53f3\u4fa7\u788e\u7247\u4e2d\u3002\u60a8\u53ef\u4ee5\u4ece\u4e0b\u9762\u7684\u94fe\u63a5\u4e0b\u8f7d\u6700\u7ec8\u7684Android\u788e\u7247\u9879\u76ee\u3002<\/p>\n<p><a href=\"#\">\u4e0b\u8f7dAndroid\u788e\u7247\u9879\u76ee<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u6211\u4eec\u5c06\u5b66\u4e60Android\u7247\u6bb5\u751f\u547d\u5468\u671f\uff0c\u5e76\u5728Android\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5b9e\u73b0\u4e00\u4e2a\u5305\u542b\u4e24\u4e2a\u7247\u6bb5\u7684\u5355\u4e00\u6d3b\u52a8\u7c7b\u3002 \u5b89\u5353 [&hellip;]<\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[900,904,1069,1070,1071],"class_list":{"0":"post-306","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-uncategorized","7":"tag-android","9":"tag-fragment","10":"tag-1070","11":"tag-1071"},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Android\u788e\u7247\u751f\u547d\u5468\u671f\u8be6\u89e3\uff1a\u5f00\u53d1\u5fc5\u5907\u77e5\u8bc6\u70b9\u4e0e\u6700\u4f73\u5b9e\u8df5 - Blog - Silicon Cloud<\/title>\n<meta name=\"description\" content=\"\u6df1\u5165\u89e3\u6790Android\u788e\u7247(Fragment)\u7684\u751f\u547d\u5468\u671f\u5404\u9636\u6bb5\uff0c\u5305\u62ec\u521b\u5efa\u3001\u8fd0\u884c\u3001\u6682\u505c\u3001\u505c\u6b62\u548c\u9500\u6bc1\u8fc7\u7a0b\u3002\u638c\u63e1\u788e\u7247\u4e0eActivity\u751f\u547d\u5468\u671f\u5173\u7cfb\uff0c\u5b66\u4e60\u788e\u7247\u7ba1\u7406\u6700\u4f73\u5b9e\u8df5\uff0c\u63d0\u5347Android\u5e94\u7528\u5f00\u53d1\u6280\u80fd\u3002\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/zh\/blog\/android\u788e\u7247\u7684\u751f\u547d\u5468\u671f\/\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Android\u788e\u7247\u751f\u547d\u5468\u671f\u8be6\u89e3\uff1a\u5f00\u53d1\u5fc5\u5907\u77e5\u8bc6\u70b9\u4e0e\u6700\u4f73\u5b9e\u8df5\" \/>\n<meta property=\"og:description\" content=\"\u6df1\u5165\u89e3\u6790Android\u788e\u7247(Fragment)\u7684\u751f\u547d\u5468\u671f\u5404\u9636\u6bb5\uff0c\u5305\u62ec\u521b\u5efa\u3001\u8fd0\u884c\u3001\u6682\u505c\u3001\u505c\u6b62\u548c\u9500\u6bc1\u8fc7\u7a0b\u3002\u638c\u63e1\u788e\u7247\u4e0eActivity\u751f\u547d\u5468\u671f\u5173\u7cfb\uff0c\u5b66\u4e60\u788e\u7247\u7ba1\u7406\u6700\u4f73\u5b9e\u8df5\uff0c\u63d0\u5347Android\u5e94\u7528\u5f00\u53d1\u6280\u80fd\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/zh\/blog\/android\u788e\u7247\u7684\u751f\u547d\u5468\u671f\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-22T22:32:02+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-08-01T12:26:50+00:00\" \/>\n<meta name=\"author\" content=\"\u96c5, \u609f\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"\u96c5, \u609f\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/android%e7%a2%8e%e7%89%87%e7%9a%84%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f\/\",\"url\":\"https:\/\/www.silicloud.com\/zh\/blog\/android%e7%a2%8e%e7%89%87%e7%9a%84%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f\/\",\"name\":\"Android\u788e\u7247\u751f\u547d\u5468\u671f\u8be6\u89e3\uff1a\u5f00\u53d1\u5fc5\u5907\u77e5\u8bc6\u70b9\u4e0e\u6700\u4f73\u5b9e\u8df5 - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/#website\"},\"datePublished\":\"2022-11-22T22:32:02+00:00\",\"dateModified\":\"2025-08-01T12:26:50+00:00\",\"author\":{\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/f044a4b7fa4ee2701702942002419ca6\"},\"description\":\"\u6df1\u5165\u89e3\u6790Android\u788e\u7247(Fragment)\u7684\u751f\u547d\u5468\u671f\u5404\u9636\u6bb5\uff0c\u5305\u62ec\u521b\u5efa\u3001\u8fd0\u884c\u3001\u6682\u505c\u3001\u505c\u6b62\u548c\u9500\u6bc1\u8fc7\u7a0b\u3002\u638c\u63e1\u788e\u7247\u4e0eActivity\u751f\u547d\u5468\u671f\u5173\u7cfb\uff0c\u5b66\u4e60\u788e\u7247\u7ba1\u7406\u6700\u4f73\u5b9e\u8df5\uff0c\u63d0\u5347Android\u5e94\u7528\u5f00\u53d1\u6280\u80fd\u3002\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/android%e7%a2%8e%e7%89%87%e7%9a%84%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f\/#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/zh\/blog\/android%e7%a2%8e%e7%89%87%e7%9a%84%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/android%e7%a2%8e%e7%89%87%e7%9a%84%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.silicloud.com\/zh\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Android\u788e\u7247\u751f\u547d\u5468\u671f\u8be6\u89e3\uff1a\u5f00\u53d1\u5fc5\u5907\u77e5\u8bc6\u70b9\u4e0e\u6700\u4f73\u5b9e\u8df5\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/zh\/blog\/\",\"name\":\"Blog - Silicon Cloud\",\"description\":\"\",\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/f044a4b7fa4ee2701702942002419ca6\",\"name\":\"\u96c5, \u609f\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e71a913e914f1aad1efc391f92084294bac54bc782acd289638580134cf667a6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e71a913e914f1aad1efc391f92084294bac54bc782acd289638580134cf667a6?s=96&d=mm&r=g\",\"caption\":\"\u96c5, \u609f\"},\"url\":\"https:\/\/www.silicloud.com\/zh\/blog\/author\/yawu\/\"},{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/android%e7%a2%8e%e7%89%87%e7%9a%84%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f\/#local-main-organization-logo\",\"url\":\"\",\"contentUrl\":\"\",\"caption\":\"Blog - Silicon Cloud\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Android\u788e\u7247\u751f\u547d\u5468\u671f\u8be6\u89e3\uff1a\u5f00\u53d1\u5fc5\u5907\u77e5\u8bc6\u70b9\u4e0e\u6700\u4f73\u5b9e\u8df5 - Blog - Silicon Cloud","description":"\u6df1\u5165\u89e3\u6790Android\u788e\u7247(Fragment)\u7684\u751f\u547d\u5468\u671f\u5404\u9636\u6bb5\uff0c\u5305\u62ec\u521b\u5efa\u3001\u8fd0\u884c\u3001\u6682\u505c\u3001\u505c\u6b62\u548c\u9500\u6bc1\u8fc7\u7a0b\u3002\u638c\u63e1\u788e\u7247\u4e0eActivity\u751f\u547d\u5468\u671f\u5173\u7cfb\uff0c\u5b66\u4e60\u788e\u7247\u7ba1\u7406\u6700\u4f73\u5b9e\u8df5\uff0c\u63d0\u5347Android\u5e94\u7528\u5f00\u53d1\u6280\u80fd\u3002","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/zh\/blog\/android\u788e\u7247\u7684\u751f\u547d\u5468\u671f\/","og_locale":"zh_CN","og_type":"article","og_title":"Android\u788e\u7247\u751f\u547d\u5468\u671f\u8be6\u89e3\uff1a\u5f00\u53d1\u5fc5\u5907\u77e5\u8bc6\u70b9\u4e0e\u6700\u4f73\u5b9e\u8df5","og_description":"\u6df1\u5165\u89e3\u6790Android\u788e\u7247(Fragment)\u7684\u751f\u547d\u5468\u671f\u5404\u9636\u6bb5\uff0c\u5305\u62ec\u521b\u5efa\u3001\u8fd0\u884c\u3001\u6682\u505c\u3001\u505c\u6b62\u548c\u9500\u6bc1\u8fc7\u7a0b\u3002\u638c\u63e1\u788e\u7247\u4e0eActivity\u751f\u547d\u5468\u671f\u5173\u7cfb\uff0c\u5b66\u4e60\u788e\u7247\u7ba1\u7406\u6700\u4f73\u5b9e\u8df5\uff0c\u63d0\u5347Android\u5e94\u7528\u5f00\u53d1\u6280\u80fd\u3002","og_url":"https:\/\/www.silicloud.com\/zh\/blog\/android\u788e\u7247\u7684\u751f\u547d\u5468\u671f\/","og_site_name":"Blog - Silicon Cloud","article_published_time":"2022-11-22T22:32:02+00:00","article_modified_time":"2025-08-01T12:26:50+00:00","author":"\u96c5, \u609f","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"\u96c5, \u609f","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"1 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/zh\/blog\/android%e7%a2%8e%e7%89%87%e7%9a%84%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f\/","url":"https:\/\/www.silicloud.com\/zh\/blog\/android%e7%a2%8e%e7%89%87%e7%9a%84%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f\/","name":"Android\u788e\u7247\u751f\u547d\u5468\u671f\u8be6\u89e3\uff1a\u5f00\u53d1\u5fc5\u5907\u77e5\u8bc6\u70b9\u4e0e\u6700\u4f73\u5b9e\u8df5 - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/zh\/blog\/#website"},"datePublished":"2022-11-22T22:32:02+00:00","dateModified":"2025-08-01T12:26:50+00:00","author":{"@id":"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/f044a4b7fa4ee2701702942002419ca6"},"description":"\u6df1\u5165\u89e3\u6790Android\u788e\u7247(Fragment)\u7684\u751f\u547d\u5468\u671f\u5404\u9636\u6bb5\uff0c\u5305\u62ec\u521b\u5efa\u3001\u8fd0\u884c\u3001\u6682\u505c\u3001\u505c\u6b62\u548c\u9500\u6bc1\u8fc7\u7a0b\u3002\u638c\u63e1\u788e\u7247\u4e0eActivity\u751f\u547d\u5468\u671f\u5173\u7cfb\uff0c\u5b66\u4e60\u788e\u7247\u7ba1\u7406\u6700\u4f73\u5b9e\u8df5\uff0c\u63d0\u5347Android\u5e94\u7528\u5f00\u53d1\u6280\u80fd\u3002","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/zh\/blog\/android%e7%a2%8e%e7%89%87%e7%9a%84%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f\/#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/zh\/blog\/android%e7%a2%8e%e7%89%87%e7%9a%84%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/zh\/blog\/android%e7%a2%8e%e7%89%87%e7%9a%84%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.silicloud.com\/zh\/blog\/"},{"@type":"ListItem","position":2,"name":"Android\u788e\u7247\u751f\u547d\u5468\u671f\u8be6\u89e3\uff1a\u5f00\u53d1\u5fc5\u5907\u77e5\u8bc6\u70b9\u4e0e\u6700\u4f73\u5b9e\u8df5"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/zh\/blog\/#website","url":"https:\/\/www.silicloud.com\/zh\/blog\/","name":"Blog - Silicon Cloud","description":"","inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/f044a4b7fa4ee2701702942002419ca6","name":"\u96c5, \u609f","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e71a913e914f1aad1efc391f92084294bac54bc782acd289638580134cf667a6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e71a913e914f1aad1efc391f92084294bac54bc782acd289638580134cf667a6?s=96&d=mm&r=g","caption":"\u96c5, \u609f"},"url":"https:\/\/www.silicloud.com\/zh\/blog\/author\/yawu\/"},{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.silicloud.com\/zh\/blog\/android%e7%a2%8e%e7%89%87%e7%9a%84%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f\/#local-main-organization-logo","url":"","contentUrl":"","caption":"Blog - Silicon Cloud"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/posts\/306","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/comments?post=306"}],"version-history":[{"count":4,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/posts\/306\/revisions"}],"predecessor-version":[{"id":109796,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/posts\/306\/revisions\/109796"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/media?parent=306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/categories?post=306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/tags?post=306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}