关于android fragment 某些使用记录

1.首先是当android2.3.3之前还是用着android-support-v4.jar来加载Fragment时。

a.在xml布局应该如何定义呢?

答案:用FrameLayout标签来定义(在android 3.0之后的版本就是好似用fragment标签)

本人尝试用其他Layout,但是类似不行(不知道是不是人品问题,不过用FrameLayout的话100%可以)

然后就以下面的代码来加载Fragment!

FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();

transaction.replace(R.id.my_frameLayout,new Fragment());

transaction.commit();

2.本人曾经出现一个问题:就是布局在上面的Fragment(就是FrameLayout)死活都要盖住他的下面的Fragment的内容。

当时一时半刻不管怎么改。就是弄不好!

这两天人品爆发。尝试把布局换一下位置。居然好了!

我贴一下xml的代码吧!

之前的代码:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical" >

<FrameLayout

  android:id="@+id/title_fragment"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >

  <FrameLayout
    android:id="@+id/content_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
    android:id="@+id/bottom_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    </FrameLayout>
  </FrameLayout>

</FrameLayout>

</LinearLayout>

更改后:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical" >

<FrameLayout

  android:id="@+id/title_fragment"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >

  </FrameLayout>

<FrameLayout
    android:id="@+id/content_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

  </FrameLayout>

<FrameLayout
    android:id="@+id/bottom_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    </FrameLayout>

</LinearLayout>

后来才知道这个FrameLayout(假如加载Fragment的话)里面是尽量不要放子控件的,否则加载Fragment就会把里面的子控件内容挡住。

3.这两天更新了android-SDK-4.4.2,就顺便更新了ADT(ADT不知道是什么的话。我劝你还是别看了)

然后今天打包时突然很多类都打了红叉。打开一看错误是:

This fragment should provide a default constructor (a public constructor with no arguments) 

大概意思是说。用这个fargment时。一定要留一个什么参数都不给的的构造方法!

然后我又去看看官方写的demo。基本都是 有

public static Fragment getInstance(boolean isTest) {
  TestFragment instance = new TestFragment();

  instance.isTest = isTest;
  return instance;
}

类似的static 方法。

大概意思就是说:传参只能等对象实例化后才能赋值!

这个不算错误。只是过不了android的打包工具检查而已!

也算android强制要求代码要规范吧!

原文地址:https://www.cnblogs.com/shortboy/p/3640660.html