Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何更改我的片段中的文件XML属性_Java_Android_Android Studio_Android Fragments - Fatal编程技术网

Java 如何更改我的片段中的文件XML属性

Java 如何更改我的片段中的文件XML属性,java,android,android-studio,android-fragments,Java,Android,Android Studio,Android Fragments,我在ViewPager中创建了四个片段类并设置了适配器 我有一个XML标准文件 测试片段.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent"

我在ViewPager中创建了四个片段类并设置了适配器 我有一个XML标准文件

测试片段.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/layout">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Egyptian Pyramids"
        android:textSize="30sp"
        android:id="@+id/textTitle"
        android:textColor="@color/colorPrimary"
            />

    <TextView
        android:id="@+id/textDescription"
        android:layout_width="match_parent"
        android:layout_height="171dp"
        android:padding="10dp"
        android:text="@string/description"
        android:textSize="12sp"
        android:textColor="#000000"/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:layout_weight="1">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/image1"/>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/image2"/>
</LinearLayout>
</LinearLayout> 


如何更改
test_fragment.xml
文件中的属性,以便在根据每个片段刷卡时显示这些属性如果我理解正确,您希望对所有片段使用相同的布局,但在TextView上使用不同的运行时值

为了方便起见,您必须创建多个片段类及其布局文件。否则,您可以使用相同的布局(xml)文件,并在已创建的res.Fragment类中更改它们的值(如您所述)

例如。
FirstFragment->fragment.xml
SecondFragment->fragment.xml
ThirdFragment->fragment.xml
第四个片段->fragment.xml

在各个片段类的onViewCreated方法中,更改“xml属性”

还是更好的

FirstFragment->fragment\u one.xml
SecondFragment->fragment_two.xml
ThirdFragment->fragment_three.xml
FourthFragment->fragment_-four.xml

您可以通过以下方式实现:

1) 在片段XML中(每个片段都有自己的XML)。给每个片段赋予不同的文本和图像属性

2) 或者,如果希望所有片段都有一个XML,只需以编程方式更改属性:

TEXT_VIEW.setText("...") 
IMAGE.setImageResource(....)

每次滑动到新片段时,您都可以通过以下内容捕捉到您的滑动:
addOnPageChangeListener

当您说要“更改属性…”是什么意思?我想根据
FirstFragment
中的每个片段示例“欢迎使用第一个片段”和
SecondFragment
“欢迎来到第二个片段”
TEXT_VIEW.setText("...") 
IMAGE.setImageResource(....)