fragment使用注意
来源:互联网 发布:@@error mysql 编辑:程序博客网 时间:2024/06/04 21:53
<span style="font-size:48px;color:#ff0000;"><strong>一</strong></span>用的是android.app中的fragment
一种是在xml中直接写明name
如:
<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="vertical" tools:context="com.example.myonpencvtestone.load.ContentActivity" > <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="@drawable/border_content_bg" android:orientation="horizontal" > <Button android:id="@+id/add" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:layout_marginLeft="24dp" android:layout_marginRight="24dp" android:layout_marginTop="12dp" android:layout_marginBottom="12dp" android:text="dsfdf" /> <Button android:id="@+id/judg" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:layout_marginLeft="24dp" android:layout_marginRight="24dp" android:layout_marginTop="12dp" android:layout_marginBottom="12dp" android:text="dsfdf" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="3" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:background="@drawable/border_content_bg" > <fragment android:name="com.example.myonpencvtestone.load.fragment.CameraFragment" android:id="@+id/camera_fragment" android:layout_weight="2" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="2" android:background="@drawable/border_content_bg" > <Button android:id="@+id/adsf" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:layout_marginLeft="24dp" android:layout_marginRight="24dp" android:layout_marginTop="12dp" android:layout_marginBottom="12dp" android:text="dsfdf" /> </LinearLayout></LinearLayout>
只需要这样就可以显示了,
注意可能会提示这个错误
Caused by: java.lang.IllegalStateException: Fragment com.example.myonpencvtestone.load.fragment.CameraFragment did not create a view.
Caused by: android.view.InflateException: Binary XML file line #46: Error inflating class fragment
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myonpencvtestone/com.example.myonpencvtestone.load.ContentActivity}: android.view.InflateException: Binary XML file line #46: Error inflating class fragment
这个是因为没有在fragment中的oncreateview中指明fragment显示的是哪个layout
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stub//return super.onCreateView(inflater, container, savedInstanceState);return inflater.inflate(R.layout.camera_fragment, container,false);}
这样就行了
二
在代码中添加
package com.example.myonpencvtestone.load;import com.example.myonpencvtestone.R;import com.example.myonpencvtestone.R.id;import com.example.myonpencvtestone.R.layout;import com.example.myonpencvtestone.R.menu;import com.example.myonpencvtestone.load.fragment.CameraFragment;import android.app.Activity;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;public class ContentActivity extends Activity {<span style="white-space:pre"></span>@Override<span style="white-space:pre"></span>protected void onCreate(Bundle savedInstanceState) {<span style="white-space:pre"></span>super.onCreate(savedInstanceState);<span style="white-space:pre"></span>setContentView(R.layout.activity_content);<span style="white-space:pre"></span>//<span style="white-space:pre"></span>//在程序中加入Fragment FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); CameraFragment fragment = new CameraFragment(); fragmentTransaction.add(R.id.camera_fragment, fragment); fragmentTransaction.commit();<span style="white-space:pre"></span>}<span style="white-space:pre"></span>@Override<span style="white-space:pre"></span>public boolean onCreateOptionsMenu(Menu menu) {<span style="white-space:pre"></span>// Inflate the menu; this adds items to the action bar if it is present.<span style="white-space:pre"></span>getMenuInflater().inflate(R.menu.content, menu);<span style="white-space:pre"></span>return true;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>@Override<span style="white-space:pre"></span>public boolean onOptionsItemSelected(MenuItem item) {<span style="white-space:pre"></span>// Handle action bar item clicks here. The action bar will<span style="white-space:pre"></span>// automatically handle clicks on the Home/Up button, so long<span style="white-space:pre"></span>// as you specify a parent activity in AndroidManifest.xml.<span style="white-space:pre"></span>int id = item.getItemId();<span style="white-space:pre"></span>if (id == R.id.action_settings) {<span style="white-space:pre"></span>return true;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>return super.onOptionsItemSelected(item);<span style="white-space:pre"></span>}}
<FrameLayout android:id="@+id/camera_fragment" android:layout_weight="2" android:layout_width="match_parent" android:layout_height="match_parent" />
0 0
- android fragment使用注意
- fragment使用注意
- fragment的使用注意问题
- ViewPager 和 Fragment 使用注意的问题
- FragmentTabHost使用注意:tab居上居下,分割线,Fragment缓存
- Fragment使用过程中一些需要注意的点
- fragment注意点
- Fragment嵌套注意问题
- 14 Fragment 注意点
- fragment 嵌套 fragment 需要注意的地方
- Fragment嵌套时注意点
- Fragment 使用
- fragment 使用
- Fragment使用
- fragment使用
- Fragment使用
- Fragment使用
- fragment使用
- 【Leetcode】 Intersection of two linked list
- 冒泡排序
- ERROR:transport error 202: gethostbyname: unknown host
- [socket]如何解除绑定bind()?
- 【深入浅出IOS开发】详解IOS中的程序的启动
- fragment使用注意
- Extjs GridPanel用法详解
- Eclipse 常用技巧
- java类的生命周期
- 修改mesh的clolors属性
- Swift字典的简单使用
- 014:字符串:各种其奇葩的内置方法
- IO流第七课,字符流、纯文本、节点流,Reader、FileReader、Writer、FileWriter读取、写出、拷贝
- 【深入浅出IOS开发】Xcode 6如何创建一个空的Empty Application