1

我创建了一个具有自定义 ImageView 布局的首选项,如下所示:

<Preference
        android:key="profile_picture_preference"
        android:layout="@layout/layout_profile_picture"></Preference>

我的布局是这样的:

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/profileImageView"
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_marginBottom="10dp"
    android:layout_marginTop="10dp"
    android:background="#ABCDEF" />

但我不知道如何获得它的参考。我试图通过这样做在 onCreateView 方法中得到它:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = super.onCreateView(inflater, container, savedInstanceState);

    ImageView imageView = (ImageView) view.findViewById(R.id.profileImageView);

    return view;
}

但 ImageView 为空。

谢谢你的帮助。

4

3 回答 3

1

您可以在主题中指定自定义布局。在那里你可以添加你的ImageView

例如:

样式.xml

<style name="YourTheme" parent="Theme.AppCompat">
    <!-- ... -->
    <item name="preferenceTheme">@style/YourTheme.PreferenceThemeOverlay</item>
</style>

<style name="YourTheme.PreferenceThemeOverlay" parent="@style/PreferenceThemeOverlay">
    <item name="android:layout">@layout/fragment_your_preferences</item>
</style>

fragment_your_preferences.xml

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

    <ImageView
        android:id="@+id/profileImageView"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:background="#ABCDEF" />

    <!-- Required ViewGroup for PreferenceFragmentCompat -->
    <FrameLayout
        android:id="@+id/list_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </FrameLayout>

</LinearLayout>

然后在onViewCreated()您的片段类中,您可以开始使用ImageView

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
{
    super.onViewCreated(view, savedInstanceState);

    ImageView image_view = (ImageView)view.findViewById(R.id.profileImageView);

    if (image_view != null)
    {
        // Your code
    }
}
于 2016-03-29T13:58:27.357 回答
1

我没有找到任何正式的方式。所以我这样管理:

@Override
public RecyclerView onCreateRecyclerView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {

    recyclerView = super.onCreateRecyclerView(inflater, parent, savedInstanceState);
    recyclerView.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
        @Override
        public void onChildViewAttachedToWindow(View view) {
            if (view.getId() == R.id.profileImageView) {
                profileImageView = (ImageView) view;
            }
        }
        @Override
        public void onChildViewDetachedFromWindow(View view) {
        }
    });
    return recyclerView;
}
于 2015-10-19T06:44:01.800 回答
0

我花了一天的时间试图解决这个问题......这是我的答案:我一直在使用onCreateView而不是onCreatePreferences加载视图:

public static class MyFragment extends PreferenceFragmentCompat {

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        //setPreferencesFromResource(R.xml.my_fragment_xml, rootKey); //remove this (!)
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.my_layout, container, false); //root layout from the fragment xml
        view myView = view.findViewById(R.id.myView); //this works (!)

        return view;
    }

}
于 2022-02-25T17:42:56.530 回答