Android 为什么不能显示片段布局?

Android 为什么不能显示片段布局?,android,android-layout,android-fragments,android-activity,fragment,Android,Android Layout,Android Fragments,Android Activity,Fragment,我错过了什么?我想从edit活动中打开edit\u信息片段。它可以显示Toast,但不显示编辑信息布局 编辑 listViewEdit.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> listView, View view,

我错过了什么?我想从
edit
活动中打开
edit\u信息
片段。它可以显示
Toast
,但不显示
编辑信息
布局

编辑

  listViewEdit.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> listView, View view,
                                    int position, long id) {
                // Get the cursor, positioned to the corresponding listview_item_row in the result set
                Cursor cursor = (Cursor) listView.getItemAtPosition(position);

                // Get the state's capital from this listview_item_row in the database.
                String ID =
                        cursor.getString(cursor.getColumnIndexOrThrow("_id"));
                EditInformation fragment2 = new EditInformation();
                Bundle bundle = new Bundle();
                bundle.putString("ID", ID);
                fragment2.setArguments(bundle);
                FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.fragment1, fragment2);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();

            }
        });
<?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"
    android:id="@+id/fragment1">

    <RelativeLayout
        android:layout_width="209dp"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/tomato"
            android:paddingLeft="10sp"
            android:textSize="20sp"
            android:paddingTop="10sp"
             android:hint="Long Press to delete"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/tomato"
            android:paddingTop="40sp"
            android:textSize="20sp"
            android:paddingLeft="10sp"
            android:hint="Hint to edit"/>

    </RelativeLayout>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listViewEdit"
        android:paddingTop="10sp"/>
</LinearLayout>
编辑

  listViewEdit.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> listView, View view,
                                    int position, long id) {
                // Get the cursor, positioned to the corresponding listview_item_row in the result set
                Cursor cursor = (Cursor) listView.getItemAtPosition(position);

                // Get the state's capital from this listview_item_row in the database.
                String ID =
                        cursor.getString(cursor.getColumnIndexOrThrow("_id"));
                EditInformation fragment2 = new EditInformation();
                Bundle bundle = new Bundle();
                bundle.putString("ID", ID);
                fragment2.setArguments(bundle);
                FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.fragment1, fragment2);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();

            }
        });
<?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"
    android:id="@+id/fragment1">

    <RelativeLayout
        android:layout_width="209dp"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/tomato"
            android:paddingLeft="10sp"
            android:textSize="20sp"
            android:paddingTop="10sp"
             android:hint="Long Press to delete"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/tomato"
            android:paddingTop="40sp"
            android:textSize="20sp"
            android:paddingLeft="10sp"
            android:hint="Hint to edit"/>

    </RelativeLayout>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listViewEdit"
        android:paddingTop="10sp"/>
</LinearLayout>

编辑信息

public class EditInformation extends Fragment {

    String ID;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View edit_info = inflater.inflate(R.layout.edit_information, container, false);
        Bundle bundle = this.getArguments();
        if(getArguments()!=null)
        {
            ID=bundle.getString("ID");
        }

        Toast.makeText(getActivity(),"Edit_Information",Toast.LENGTH_LONG).show();
        return edit_info;
    }
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10sp"
    android:orientation="vertical"
    android:background="@color/beige">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/Name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/round_corner_up"
            android:padding="10dp"
            android:text="Name"
            android:textColor="@color/tomato"
            android:textSize="20dp" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/Name"
            android:layout_marginTop="-15dp"
            android:background="@drawable/round_corner_up">

            <Spinner
                android:layout_width="339dp"
                android:layout_height="52dp"
                android:id="@+id/SpinnerName" />

        </LinearLayout>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="10dp">

        <TextView
            android:id="@+id/Weather"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/round_corner_up"
            android:padding="10dp"
            android:text="Weather"
            android:textColor="@color/tomato"
            android:textSize="20dp" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/Weather"
            android:layout_marginTop="-15dp"
            android:background="@drawable/round_corner_up">
            <Spinner
                android:layout_width="342dp"
                android:layout_height="52dp"
                android:id="@+id/SpinnerWeather">

            </Spinner>

        </LinearLayout>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="10dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/Date"
            android:background="@drawable/round_corner_up"
            android:padding="10dp"
            android:text="Date"
            android:textColor="@color/tomato"
            android:textSize="20dp"/>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/Date"
            android:layout_marginTop="-15dp"
            android:background="@drawable/round_corner_up">

            <EditText
                android:layout_width="311dp"
                android:layout_height="52dp"
                android:layout_margin="10sp"
                android:background="@drawable/round_corner_square"
                android:id="@+id/date"
                android:paddingLeft="20dp" />

        </LinearLayout>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="10dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/Status"
            android:background="@drawable/round_corner_up"
            android:padding="10dp"
            android:text="Status"
            android:textColor="@color/tomato"
            android:textSize="20dp"/>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/Status"
            android:layout_marginTop="-15dp"
            android:background="@drawable/round_corner_up">
            <Spinner
                android:layout_width="339dp"
                android:layout_height="52dp"
                android:id="@+id/SpinnerStatus">

            </Spinner>
        </LinearLayout>

    </RelativeLayout>
    <AbsoluteLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="20sp"
        android:orientation="horizontal">
        <Button
            android:layout_width="187dp"
            android:layout_weight="0.83"
            android:text="Add Work Force"
            android:textSize="20sp"
            android:drawableRight="@mipmap/add_workforce"
            android:layout_height="75dp"
            android:id="@+id/AddForce"
            android:layout_x="12dp"
            android:layout_y="11dp" />

        <Button
            android:layout_width="123dp"
            android:layout_weight="0.83"
            android:text="Next"
            android:textSize="20sp"
            android:drawableRight="@mipmap/next"
            android:layout_height="71dp"
            android:id="@+id/Next"
            android:layout_x="211dp"
            android:layout_y="14dp" />
    </AbsoluteLayout>
</LinearLayout>


尝试使用
FrameLayout
作为片段的容器,而不是
LinearLayout
。我相信,由于
LinearLayout
您的片段是在屏幕外创建的。其中as
FrameLayout
将在其他视图上重叠您的片段。

在replace()方法中使用的“R.id.fragment1”是什么?请尝试在Scrollview中的edit.xml中包装您的LinearLayout,要查看您的片段是否显示在ListView下面,我如何包装linearLayout?您只需要使用Scrollview元素作为布局的根元素。查看此答案,它将帮助您使用FragmentLayout而不是Linear?它的FrameLayout不是FragmentLayout