android-在<;中为微调器设置文本;包括/>;

android-在<;中为微调器设置文本;包括/>;,android,spinner,Android,Spinner,我想为布局中的微调器设置文本。但是,仅设置第一个微调器。如何为具有相同id的所有微调器设置文本 另外,我想问一下,当单击imageview时,如何将另一个skillfield.xml添加到fragment_skill.xml 多谢各位 fragment_skill.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"

我想为布局中的微调器设置文本。但是,仅设置第一个微调器。如何为具有相同id的所有微调器设置文本

另外,我想问一下,当单击imageview时,如何将另一个skillfield.xml添加到fragment_skill.xml

多谢各位

fragment_skill.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/skillInfo" >

<RelativeLayout
    android:id="@+id/OCC"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/occ"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:text="@string/occSkill"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/occPt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_toEndOf="@id/occ"
        android:text=""
        android:textSize="20sp" />

    <include
        android:id="@+id/oocSkill"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/occ"
        layout="@layout/skillfield" />

    <ImageView
        android:id="@+id/addOccSkill"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_below="@id/oocSkill"
        android:contentDescription="@string/delSkill"
        android:src="@android:drawable/ic_input_add" />

</RelativeLayout>

<RelativeLayout
    android:id="@+id/ORI"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/OCC" >

    <TextView
        android:id="@+id/ori"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:text="@string/oriSkill"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/oriPt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_toEndOf="@id/ori"
        android:text=""
        android:textSize="20sp" />

    <include
        android:id="@+id/oriSkill"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/ori"
        layout="@layout/skillfield" />

    <ImageView
        android:id="@+id/addOriSkill"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_below="@id/oriSkill"
        android:contentDescription="@string/delSkill"
        android:src="@android:drawable/ic_input_add" />

</RelativeLayout>

</RelativeLayout>

skillfield.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/addSkillField" >

<Spinner
    android:id="@+id/selectSkill"
    android:layout_width="180dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="10dp" />

<TextView 
    android:id="@+id/skillPt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="10dp" 
    android:text=""
    android:textSize="20sp" />

<ImageView
    android:id="@+id/addSkill"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_gravity="bottom"
    android:contentDescription="@string/delSkill"
    android:src="@android:drawable/ic_delete" />

</LinearLayout>

在课堂上

skill = (Spinner) findViewById(R.id.selectSkill);
ArrayAdapter<CharSequence> skillAdapter = ArrayAdapter.createFromResource(context, R.array.skills, R.layout.spinner);
skillAdapter.setDropDownViewResource(R.layout.spinner_down);
skill.setAdapter(skillAdapter);
skill=(微调器)findviewbyd(R.id.selectSkill);
ArrayAdapter skillAdapter=ArrayAdapter.createFromResource(context,R.array.skills,R.layout.spinner);
skillAdapter.setDropDownViewResource(R.layout.spinner_向下);
技能设置适配器(skillAdapter);
如何使用相同ID设置
微调器

确实,如果您这样做:
skill=(微调器)findviewbyd(R.id.selectSkill)

您将只能获得第一个
微调器
。我认为您不能使用
动态添加新的
微调器

以下是如何实现所需目标的主要思路:

fragment\u skill.xml
中,您需要添加一个空容器视图。每次单击按钮时,应将此容器插入要添加
微调器的位置。此容器将容纳添加的
微调器

假设您要在此处插入:

<RelativeLayout...>
    <TextView.../>
    <TextView.../>
    <!-- here is where you want to insert your Spinner -->
    <ImageView.../>
</RelativeLayout...>
您可以根据需要添加任何其他类型的布局,它不必是
LinearLayout
。然后,从活动/片段中获取
LinearLayout
,如下所示:

<RelativeLayout...>
    <TextView.../>
    <TextView.../>
    <LinearLayout
        android:id="@+id/container"
        android:orientation="vertical
        ... />
    <ImageView.../>
</RelativeLayout...>
 LinearLayout mContainer; // make this instance variable
 mContainer = (LinearLayout)findViewById(R.id.container);
public void addNewSpinner(){
    View view = getLayoutInflater().inflate(R.layout.skillfield, null);
    Spinner skill = (Spinner) view.findViewById(R.id.selectSkill);
    ArrayAdapter<CharSequence> skillAdapter = ArrayAdapter.createFromResource(context, R.array.skills, R.layout.spinner);
    skillAdapter.setDropDownViewResource(R.layout.spinner_down);
    skill.setAdapter(skillAdapter);
    mContainer.addView(skill);
}
同时抓取按钮以便设置侦听器,假设它被称为
按钮mButton

mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            addNewSpinner();
    });
因此,当单击按钮时,您将调用
addNewSpinner()
。现在,创建一个名为
addNewSpinner()
的方法。在方法内部,我们将膨胀
skillfield.xml
。该方法应如下所示:

<RelativeLayout...>
    <TextView.../>
    <TextView.../>
    <LinearLayout
        android:id="@+id/container"
        android:orientation="vertical
        ... />
    <ImageView.../>
</RelativeLayout...>
 LinearLayout mContainer; // make this instance variable
 mContainer = (LinearLayout)findViewById(R.id.container);
public void addNewSpinner(){
    View view = getLayoutInflater().inflate(R.layout.skillfield, null);
    Spinner skill = (Spinner) view.findViewById(R.id.selectSkill);
    ArrayAdapter<CharSequence> skillAdapter = ArrayAdapter.createFromResource(context, R.array.skills, R.layout.spinner);
    skillAdapter.setDropDownViewResource(R.layout.spinner_down);
    skill.setAdapter(skillAdapter);
    mContainer.addView(skill);
}
public void addNewSpinner(){
View=GetLayoutFlater().充气(R.layout.skillfield,null);
微调器技能=(微调器)视图.findViewById(R.id.selectSkill);
ArrayAdapter skillAdapter=ArrayAdapter.createFromResource(context,R.array.skills,R.layout.spinner);
skillAdapter.setDropDownViewResource(R.layout.spinner_向下);
技能设置适配器(skillAdapter);
mContainer.addView(技能);
}
如果您不确定充气是什么意思,请参见以下说明:

将布局XML文件实例化为其相应的视图对象

希望有帮助

如何使用相同ID设置
微调器

确实,如果您这样做:
skill=(微调器)findviewbyd(R.id.selectSkill)

您将只能获得第一个
微调器
。我认为您不能使用
动态添加新的
微调器

以下是如何实现所需目标的主要思路:

fragment\u skill.xml
中,您需要添加一个空容器视图。每次单击按钮时,应将此容器插入要添加
微调器的位置。此容器将容纳添加的
微调器

假设您要在此处插入:

<RelativeLayout...>
    <TextView.../>
    <TextView.../>
    <!-- here is where you want to insert your Spinner -->
    <ImageView.../>
</RelativeLayout...>
您可以根据需要添加任何其他类型的布局,它不必是
LinearLayout
。然后,从活动/片段中获取
LinearLayout
,如下所示:

<RelativeLayout...>
    <TextView.../>
    <TextView.../>
    <LinearLayout
        android:id="@+id/container"
        android:orientation="vertical
        ... />
    <ImageView.../>
</RelativeLayout...>
 LinearLayout mContainer; // make this instance variable
 mContainer = (LinearLayout)findViewById(R.id.container);
public void addNewSpinner(){
    View view = getLayoutInflater().inflate(R.layout.skillfield, null);
    Spinner skill = (Spinner) view.findViewById(R.id.selectSkill);
    ArrayAdapter<CharSequence> skillAdapter = ArrayAdapter.createFromResource(context, R.array.skills, R.layout.spinner);
    skillAdapter.setDropDownViewResource(R.layout.spinner_down);
    skill.setAdapter(skillAdapter);
    mContainer.addView(skill);
}
同时抓取按钮以便设置侦听器,假设它被称为
按钮mButton

mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            addNewSpinner();
    });
因此,当单击按钮时,您将调用
addNewSpinner()
。现在,创建一个名为
addNewSpinner()
的方法。在方法内部,我们将膨胀
skillfield.xml
。该方法应如下所示:

<RelativeLayout...>
    <TextView.../>
    <TextView.../>
    <LinearLayout
        android:id="@+id/container"
        android:orientation="vertical
        ... />
    <ImageView.../>
</RelativeLayout...>
 LinearLayout mContainer; // make this instance variable
 mContainer = (LinearLayout)findViewById(R.id.container);
public void addNewSpinner(){
    View view = getLayoutInflater().inflate(R.layout.skillfield, null);
    Spinner skill = (Spinner) view.findViewById(R.id.selectSkill);
    ArrayAdapter<CharSequence> skillAdapter = ArrayAdapter.createFromResource(context, R.array.skills, R.layout.spinner);
    skillAdapter.setDropDownViewResource(R.layout.spinner_down);
    skill.setAdapter(skillAdapter);
    mContainer.addView(skill);
}
public void addNewSpinner(){
View=GetLayoutFlater().充气(R.layout.skillfield,null);
微调器技能=(微调器)视图.findViewById(R.id.selectSkill);
ArrayAdapter skillAdapter=ArrayAdapter.createFromResource(context,R.array.skills,R.layout.spinner);
skillAdapter.setDropDownViewResource(R.layout.spinner_向下);
技能设置适配器(skillAdapter);
mContainer.addView(技能);
}
如果您不确定充气是什么意思,请参见以下说明:

将布局XML文件实例化为其相应的视图对象

希望有帮助