在PreferenceFragmentCompat中获取自定义布局参考

时间:2015-10-17 18:26:29

标签: android fragment preferences preferencefragment

我创建了一个首选项,它具有自定义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为空。

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

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

例如:

<强> styles.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
    }
}

答案 1 :(得分:0)

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

@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;
}