2016-08-27 8 views
0

2つのフラグメントクラスを作成してから、activity_main.xmlに2つのフラグメントタグを記述し、name属性も記述しました。activity.xmlに記述されているフラグメントがエミュレータに表示されない

フラグメントクラス;

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> 

がフラグメントのために使用するレイアウトはこの1つである:

<?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> 

私は、コードを実行していますエミュレータでは私はjusですtextViewにメッセージを1回だけ表示させます。 2番目のものではなくfirstFragmentが印刷されているようです。 両方のフラグメントを印刷するにはどうすればよいですか?

+0

まあ、 'RelativeLayout'は互いの上にあなたの破片を入れている...そして、あなたのフラグメントの両方が同じレイアウトを持っている... – Shaishav

+0

@Shaishavはあなたがオーバーなぜあなたの一つの断片だ相対的なレイアウトを使用して言ったように両方を表示したい場合は、別のフラグメントを作成してください。http://www.javatpoint.com/android-fragments – Shailesh

答えて

0

重複しています。 layout_belowlayout_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" 
     /> 
関連する問題