Java 创建模板布局文件并动态添加值以将其添加到首选项中

Java 创建模板布局文件并动态添加值以将其添加到首选项中,java,android,android-fragments,android-preferences,Java,Android,Android Fragments,Android Preferences,我正在创建一个preferences活动,其中我使用一个片段来添加首选项。我正在尝试创建一个模板布局文件,并以某种方式将数据动态添加到布局文件中,并将其添加到首选项中。这不起作用,有什么想法吗 模板xml文件-首选项\u dynamic\u layout.xml generateReference.java 运行后,布局会添加到首选项中,但值不会更新。我们可以使用自定义布局并动态设置值。 使用以下XML并确保元素的id保持不变。 标题、摘要、图标 首选项\u自定义\u child.xml 我们可

我正在创建一个preferences活动,其中我使用一个片段来添加首选项。我正在尝试创建一个模板布局文件,并以某种方式将数据动态添加到布局文件中,并将其添加到首选项中。这不起作用,有什么想法吗

模板xml文件-首选项\u dynamic\u layout.xml

generateReference.java


运行后,布局会添加到首选项中,但值不会更新。我们可以使用自定义布局并动态设置值。 使用以下XML并确保元素的id保持不变。 标题、摘要、图标

首选项\u自定义\u child.xml


我们可以使用自定义布局并动态设置值。 使用以下XML并确保元素的id保持不变。 标题、摘要、图标

首选项\u自定义\u child.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/preference_dynamic_rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/kumbai"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="565"
        android:background="@color/gray" />

</RelativeLayout>
    public class SettingsFragment extends PreferenceFragment {

    PreferenceCategory preferenceCategory;
    PreferenceScreen preferenceScreen;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.preferances);

        preferenceScreen = this.getPreferenceScreen();
        preferenceCategory = new PreferenceCategory(preferenceScreen.getContext());


        Intent intent = new Intent(getActivity(), GeneratePreference.class);
        startActivityForResult(intent, 565);

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 565) {
            if (resultCode == RESULT_OK) {
                int returnString = data.getIntExtra("resID", 0);
                int ss = R.layout.preference_notification_title;
                preferenceCategory.setLayoutResource(returnString);
                preferenceScreen.addPreference(preferenceCategory);
            }
        }
    }
}
    public class GeneratePreference extends AppCompatActivity {
    String text = "Kumbaiiii.....";
    RelativeLayout relativeLayout;
    TextView textView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.preference_dynamic_layout);
        relativeLayout = findViewById(R.id.preference_dynamic_rl);
        textView = findViewById(R.id.kumbai);
//        textView = new TextView(getApplicationContext());
        textView.setText(text);

        synchronized (relativeLayout){
            relativeLayout.notify();
            relativeLayout.notifyAll();
        }

//        relativeLayout.addView(textView);

        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putInt("resID", R.layout.preference_dynamic_layout);
        intent.putExtras(bundle);
        setResult(RESULT_OK, intent);
        finish();
    }


    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public int getLayout() {
        return relativeLayout.getId();
    }
}
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingStart="16dip"
android:paddingEnd="?android:attr/scrollbarSize">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="horizontal">

    <ImageView
        android:id="@android:id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"/>
</LinearLayout>

<RelativeLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="6dip"
    android:layout_marginEnd="6dip"
    android:layout_marginBottom="6dip"
    android:layout_weight="1">

    <TextView
        android:id="@android:id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:fontFamily="@font/questrial_regular"
        android:singleLine="true"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/black"
        android:textStyle="bold" />

    <TextView
        android:id="@android:id/summary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@android:id/title"
        android:paddingStart="10dp"
        android:layout_alignStart="@android:id/title"
        android:fontFamily="@font/questrial_regular"
        android:maxLines="4"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/grayDark"/>
</RelativeLayout>
<!-- Preference should place its actual preference widget here. -->
<LinearLayout
    android:id="@android:id/widget_frame"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:animateLayoutChanges="true"
    android:gravity="center"
    android:orientation="vertical" />
</LinearLayout>
        .setLayoutResource(R.layout.preference_custom_child);