2017-04-30 5 views
-3

私はこの問題を抱えています。フラグメントを変更すると、以下のTextViewが残り、結果は2 TextViewになります。フラグメントを変更するがテキストreamin android

私はより良い説明:

私は、文字列「こんにちは言葉」

私は、フラグメントを変更したときに、私はページ全体を変更し、別のTextViewを設定したいが、結果は2 TextViewの

であるとの1つのTextViewを持っています

ありがとう!

activityMain.xml:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start"> 

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

    <include 
     layout="@layout/app_bar_main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 
     app:headerLayout="@layout/nav_header_main" 
     app:menu="@menu/activity_main_drawer"/> 

</android.support.v4.widget.DrawerLayout> 

fragment.xml:変更のMainActivityで

<?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:gravity="center" 
       android:background="#5ba4e5" 
       android:layout_height="match_parent" 
       android:id="@+id/fragment"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="40px" 
     android:textColor="#ffffff" 
     android:layout_gravity="center" 
     android:id="@+id/fragmentText" 
     /> 

</LinearLayout> 

この断片:

private void selectItem(int id) { 
     // update the main content by replacing fragments 
     Fragment fragment = new MyFragment(); 

     FragmentManager fragmentManager = getFragmentManager(); 
     fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit(); 
    } 

これはMyFragment.javaある:

public class MyFragment extends Fragment { 

    public MyFragment() { 
     // Empty constructor required for fragment subclasses 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment, container, false); 
     // String planet = getResources().getStringArray(R.array.planets_array)[i]; 
     String planet = "prova fragment"; 
     int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()), 
       "drawable", getActivity().getPackageName()); 
     ((TextView) rootView.findViewById(R.id.fragmentText)).setText(planet); 
     return rootView; 
    } 
} 
私が使用
+0

あなたのコードは何ですか?あなたがエラーを送信しようとすると、エラーが発生します... –

+0

私はコードを入れようとしますが、StackOverflowによると:「あまりにも多くのコード」-.- –

+0

私はコードを追加できません.... –

答えて

0

fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit(); 

しかしcontent_frameでframeLayoutであり、フラグメントはので、私はcontent_main.xml中のLinearLayoutのIDでcontent_frameを変更し、それが動作のLinearLayoutです。

関連する問題