Java 为什么我不能在片段中动态更新文本?

Java 为什么我不能在片段中动态更新文本?,java,android,android-layout,android-fragments,android-adapter,Java,Android,Android Layout,Android Fragments,Android Adapter,这是我的Xml代码 <?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:orientation="vertical" > <Scro

这是我的Xml代码

<?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:orientation="vertical"

>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:text="Well heres the secret !"
            android:textAlignment="center"
            android:textColor="@color/bluishblack"
            android:textSize="25sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:text="1. Secret is simple,the cards we showed you contained key numbers(very first number of the list)"
            android:textAlignment="center"
            android:textSize="23sp" />


        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="'  2 , 4 , 1 , 16 , 32 , 8 , 64  '  "
            android:textAlignment="center"
            android:textColor="@color/dgreen"
            android:textSize="23sp" />


        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:text="2. Above are the key numbers. For every card that contains spectator's number you add key number(First number of card) of respective card. "
            android:textAlignment="center"
            android:textSize="23sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:text="3. And thats how you Read People's mind ! As easy as that ! "
            android:textAlignment="center"
            android:textSize="23sp" />

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center">

            <Button
                android:id="@+id/play_again"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:onClick="playAgain"
                android:text="Play again !"
                android:textColor="@color/dgreen"

                />
        </RelativeLayout>

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/imageView"
                        android:layout_width="match_parent"
                        android:layout_height="150dp"
                        android:layout_marginBottom="8dp"
                        android:background="@drawable/googleg_standard_color_18" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="8dp"
                        android:text="Your rating helps us to put more efforts i future devlopment,also we get to know more aboout your experiences and Views !  "
                        android:textAlignment="center"
                        android:textSize="20sp" />

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="99dp"
                        android:text="Rate now !" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Still Here? Do you want to amaze your friends and family.There are many awesome tricks which will blow away peoples mind yet so simple to do See below recommendations now "
                        android:textAlignment="center"
                        android:textSize="20sp"

                        />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="8dp"
                        android:text="Do Want to amaze your Freinds !!"
                        android:textAlignment="center"
                        android:textColor="@android:color/black"
                        android:textSize="26sp" />


                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="2dp"
                        android:layout_marginLeft="12dp"
                        android:layout_marginRight="12dp"
                        android:background="@android:color/darker_gray" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="6dp"
                        android:layout_marginTop="8dp"
                        android:text="Here are some of our Suggestions :- "
                        android:textColor="@color/colorPrimaryDark"
                        android:textSize="16sp" />


                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="2dp"
                        android:layout_margin="8dp"
                        android:layout_marginLeft="12dp"
                        android:layout_marginRight="12dp"
                        android:background="@android:color/darker_gray" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_margin="8dp"
                        android:text="Swipe to see more --->"
                        android:textAlignment="center"
                        android:textSize="22sp" />


                </LinearLayout>


            </RelativeLayout>


        </ScrollView>

        <android.support.v4.view.ViewPager
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="500dp">

        </android.support.v4.view.ViewPager>

    </LinearLayout>


</ScrollView>
这是寻呼机布局的代码

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="24dp"
    >

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_icon"
        android:id="@+id/image_book"
        />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:text="This is description"
        android:textSize="20sp"
        android:id="@+id/textDes"
        />

</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    >

    <Button
        android:id="@+id/getItNow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Start amazing your friends !"
        android:textAlignment="center"

        />

</LinearLayout>


</LinearLayout>


如何动态更改文本和图像我还有三个使用相同布局(pager_布局)的片段我还可以添加不同的意图吗?

您必须在onViewCreated()方法中的文本视图中设置文本,而不是在fragment类中的onCreateView()中设置文本:

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.pager_layout, container, false);
    return view;
}

TextView tv;

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    tv = (TextView) view.findViewById(R.id.textDes);
    tv.setText("text to set dynamically");
    ...
}

您必须在onViewCreated()方法内的文本视图中设置文本,而不是在片段类的onCreateView()中设置文本:

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.pager_layout, container, false);
    return view;
}

TextView tv;

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    tv = (TextView) view.findViewById(R.id.textDes);
    tv.setText("text to set dynamically");
    ...
}