我创建了两个片段类,然后在activity_main.xml中我提到了两个片段标记,并且也提到了name属性。
FragmentClass;
package com.example.hsports.fragmentsinandroid;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by I324671 on 8/27/2016.
*/
public class FragmentClass extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragmentlayout,container,false);
}
}
FragmentClass2:
package com.example.hsports.fragmentsinandroid;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by I324671 on 8/27/2016.
*/
public class FragmentClass2 extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragmentlayout,container,false);
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.hsports.fragmentsinandroid.MainActivity">
<fragment android:name="com.example.hsports.fragmentsinandroid.FragmentClass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/firstFrag"
/>
<fragment android:name="com.example.hsports.fragmentsinandroid.FragmentClass2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/secondFrag"
/>
</RelativeLayout>
用于片段的布局是这样的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is the first one"
/>
</LinearLayout>
现在,当我在模拟器中运行代码时,我只是在textView中显示消息一次。 看起来我得到的是firstFragment,而不是第二个。 我该怎么做才能打印这两个碎片?
答案 0 :(得分:0)
他们正在重叠。使用 layout_below 和 layout_alignParentTop 属性更新了代码。
<fragment android:name="com.example.hsports.fragmentsinandroid.FragmentClass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:id="@+id/firstFrag"
/>
<fragment android:name="com.example.hsports.fragmentsinandroid.FragmentClass2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/secondFrag"
android:layout_below="@+id/firstFrag"
/>