fragment使用注意

<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

	@Override
	public 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"
            />














评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值