安卓fragmentTransaction.replace不能全屏问题

操蛋的问题,看下下面代码

package com.example.fragmenttest;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity implements OnClickListener {
	private FragmentManager fragmentManager;
	private FragmentTransaction fragmentTransaction;

	private TextView tv1;
	private TextView tv2;
	
	private Fragment1 f1;
	private Fragment2 f2;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		f1 = new Fragment1();
		f2 = new Fragment2();

		tv1 = (TextView) findViewById(R.id.tb1);
		tv2 = (TextView) findViewById(R.id.tb2);
		tv1.setOnClickListener(this);
		tv2.setOnClickListener(this);
		
		fragmentManager = getFragmentManager();
		fragmentTransaction = fragmentManager.beginTransaction();
		fragmentTransaction.replace(R.id.content, f1);
		fragmentTransaction.commit();

	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.tb1:
			fragmentTransaction = fragmentManager.beginTransaction();
			fragmentTransaction.replace(R.id.content, new Fragment1());
			fragmentTransaction.commit();
			break;
			
		case R.id.tb2:
			fragmentTransaction = fragmentManager.beginTransaction();
			fragmentTransaction.replace(R.id.content, new Fragment2());
			fragmentTransaction.commit();
			break;

		default:
			break;
		}

	}
}


上面是MainActivity的,下面是Fragment

package com.example.fragmenttest;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment1 extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.fragment1, null);
	}
}


下面是MainActivity的XML和F1的XML

<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.fragmenttest.MainActivity" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/tb1"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="红" />

        <TextView
            android:id="@+id/tb2"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="蓝" />
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/content"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </RelativeLayout>

</LinearLayout>



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0000ff"
    android:orientation="vertical" >
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="F1"
        />

</LinearLayout>


简单的代码上完了,直接看效果



妈蛋这个Fragment能不能不要这么小。其实之前我Fragment里面是没有加TextView的,然后就直接不显示了,纠错了好久。

后面查阅了一些资料,把替换的容器改为FrameLayout就可以了。下面是修改后的代码。

<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.fragmenttest.MainActivity" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/tb1"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="红" />

        <TextView
            android:id="@+id/tb2"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="蓝" />
    </LinearLayout>

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </FrameLayout>

</LinearLayout>


这样才算正常。具体原理我查下。找到后补充。

补充:http://developer.android.com/training/basics/fragments/index.html

直接看官方文档可以知道里面是规定了FrameLayout的,所以不用的话就会出现这个bug。具体的原理是怎么样的,等有空了就查查吧。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值