首页 > 解决方案 > 我没有在 Fragment 类(top_section_fragment)中获得我的片段布局参考

问题描述

这是我的片段布局 top_section_fragment.xml

  <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent" android:layout_height="match_parent"
      >`enter code here`
      <EditText
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/text1"
          android:layout_centerHorizontal="true"
          android:width="250dp"
          android:height="50dp"
          android:layout_marginTop="100dp"></EditText>

      <Button
          android:layout_width="wrap_content"
          android:layout_height="50dp"
          android:id="@+id/button1"
          android:layout_centerHorizontal="true"
          android:layout_marginTop="299dp"
          android:text="Click Here"></Button>
  </RelativeLayout>

这是我的片段类 TopSectionFragment.java

    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import androidx.annotation.NonNull;
    import androidx.annotation.Nullable;
    import androidx.fragment.app.Fragment;

    import java.util.zip.Inflater;

    public class TopSectionFragment extends Fragment {
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.top_section_fragment)
            return view;

        }
    }

我没有使用 R.layout.top_section_fragment 在片段类中获取我的片段布局

标签: javaandroidandroid-fragments

解决方案


这样做:

View view = inflater.inflate(R.layout.top_section_fragment, container, false);
return view;

推荐阅读