UI控件未在fragment上显示-android

UI控件未在fragment上显示-android,android,android-fragments,Android,Android Fragments,我是android新手。第一次使用片段。我在fragment.xml(名为fragment\u plus\u one.xml)中有button、imagebutton、edittext和textview。我创建了mainActivity。我在mainActivity中有4个按钮。单击按钮3,将打开一个新片段(如前所述)。但这些uI控件并没有显示在片段上。对于文本视图,我使用了tv.setText(“yourText”);仍然没有显示文本。如何显示它们。 activity_main.xml:

我是android新手。第一次使用片段。我在fragment.xml(名为fragment\u plus\u one.xml)中有button、imagebutton、edittext和textview。我创建了mainActivity。我在mainActivity中有4个按钮。单击按钮3,将打开一个新片段(如前所述)。但这些uI控件并没有显示在片段上。对于文本视图,我使用了tv.setText(“yourText”);仍然没有显示文本。如何显示它们。


activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:orientation="vertical" android:padding="0dp"
    android:layout_height="wrap_content" android:gravity="fill_vertical" android:layout_margin="0dp">

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button3"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button4"
        android:layout_marginTop="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginEnd="8dp" />

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="0dp"
        android:layout_height="200dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="0dp"
        android:visibility="gone" >

    </FrameLayout>

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="0dp" />

    <Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_marginRight="8dp"
        android:layout_marginTop="0dp" />
</LinearLayout>
fragment_plus_one.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:orientation="vertical" android:padding="0dp"
    android:layout_height="wrap_content" android:gravity="fill_vertical" android:layout_margin="0dp">

    <LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="fill_parent" android:orientation="vertical"
    android:gravity="fill_vertical" android:layout_margin="10dp">

        <EditText
            android:id="@+id/seat_serial_prefix"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-"
            android:lines="1"
            android:hint="Seat Serial Prefix">
        </EditText>

        <EditText
            android:id="@+id/seat_serial_start_number"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:inputType="number"
            android:lines="1"
            android:hint="Seat Serial Start Number">
        </EditText>

        <EditText
            android:id="@+id/seat_serial_end_number"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:inputType="number"
            android:lines="1"
            android:hint="Seat Serial End Number">
        </EditText>

        <Button
            android:id="@+id/button_Generate"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="Generate" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:orientation="horizontal" android:padding="0dp"
        android:layout_height="wrap_content" android:gravity="fill_horizontal" android:layout_margin="0dp">

        <FrameLayout android:layout_height="wrap_content"
            android:layout_width="wrap_content" android:orientation="vertical"
            android:gravity="fill_vertical" android:layout_margin="10dp">

            <ImageButton
                android:id="@+id/imageButtonGenerateBulk"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="25dp"
                android:layout_marginBottom="40dp"
                android:layout_marginTop="0dp"
                android:layout_gravity="center_vertical"
                android:ellipsize="end"
                android:scrollHorizontally="true"
                android:singleLine="true"
                android:text="Generate Qr Code"
                android:textStyle="bold"
                android:layout_alignEnd="@+id/tv_question"
                android:layout_alignRight="@+id/tv_question"
                android:background="#00ffffff"
                android:src="@drawable/qr_code"
                android:layout_weight="1"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:layout_marginBottom="65dp"
                android:layout_marginTop="65dp"
                android:clickable="false"
                android:text="Generate" >
            </TextView>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:layout_marginBottom="45dp"
            android:layout_marginTop="85dp"
            android:clickable="false"
            android:text="Bulk" >
        </TextView>
    </FrameLayout>

    <FrameLayout android:layout_height="wrap_content"
        android:layout_width="wrap_content" android:orientation="vertical"
        android:gravity="fill_vertical" android:layout_margin="10dp">

        <ImageButton
            android:id="@+id/imageButtonGenerateSingle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="25dp"
            android:layout_marginBottom="40dp"
            android:layout_marginTop="0dp"
            android:layout_gravity="center_vertical"
            android:ellipsize="end"
            android:scrollHorizontally="true"
            android:singleLine="true"
            android:text="Generate Qr Code"
            android:textStyle="bold"
            android:layout_alignEnd="@+id/tv_question"
            android:layout_alignRight="@+id/tv_question"
            android:background="#00ffffff"
            android:src="@drawable/qr_code"
            android:layout_weight="1"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:layout_marginBottom="65dp"
            android:layout_marginTop="65dp"
            android:clickable="false"
            android:text="Generate" >
        </TextView>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:layout_marginBottom="45dp"
            android:layout_marginTop="85dp"
            android:clickable="false"
            android:text="Single" >
        </TextView>
    </FrameLayout>

    </LinearLayout>
</LinearLayout>

为什么“@+id/frameLayout”宽度设置为0dp?它应该设置为包装内容或填充父项。

为什么“@+id/frameLayout”宽度设置为0dp?它应该设置为包装内容或填充父项。

activity\u main
FrameLayout
0dp
值更改为
wrap\u content
,然后一切都会正常工作

activity\u main
FrameLayout
0dp
值更改为
wrap\u content
,然后一切都会正常工作

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:orientation="vertical" android:padding="0dp"
    android:layout_height="wrap_content" android:gravity="fill_vertical" android:layout_margin="0dp">

    <LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="fill_parent" android:orientation="vertical"
    android:gravity="fill_vertical" android:layout_margin="10dp">

        <EditText
            android:id="@+id/seat_serial_prefix"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-"
            android:lines="1"
            android:hint="Seat Serial Prefix">
        </EditText>

        <EditText
            android:id="@+id/seat_serial_start_number"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:inputType="number"
            android:lines="1"
            android:hint="Seat Serial Start Number">
        </EditText>

        <EditText
            android:id="@+id/seat_serial_end_number"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:inputType="number"
            android:lines="1"
            android:hint="Seat Serial End Number">
        </EditText>

        <Button
            android:id="@+id/button_Generate"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="Generate" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:orientation="horizontal" android:padding="0dp"
        android:layout_height="wrap_content" android:gravity="fill_horizontal" android:layout_margin="0dp">

        <FrameLayout android:layout_height="wrap_content"
            android:layout_width="wrap_content" android:orientation="vertical"
            android:gravity="fill_vertical" android:layout_margin="10dp">

            <ImageButton
                android:id="@+id/imageButtonGenerateBulk"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="25dp"
                android:layout_marginBottom="40dp"
                android:layout_marginTop="0dp"
                android:layout_gravity="center_vertical"
                android:ellipsize="end"
                android:scrollHorizontally="true"
                android:singleLine="true"
                android:text="Generate Qr Code"
                android:textStyle="bold"
                android:layout_alignEnd="@+id/tv_question"
                android:layout_alignRight="@+id/tv_question"
                android:background="#00ffffff"
                android:src="@drawable/qr_code"
                android:layout_weight="1"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:layout_marginBottom="65dp"
                android:layout_marginTop="65dp"
                android:clickable="false"
                android:text="Generate" >
            </TextView>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:layout_marginBottom="45dp"
            android:layout_marginTop="85dp"
            android:clickable="false"
            android:text="Bulk" >
        </TextView>
    </FrameLayout>

    <FrameLayout android:layout_height="wrap_content"
        android:layout_width="wrap_content" android:orientation="vertical"
        android:gravity="fill_vertical" android:layout_margin="10dp">

        <ImageButton
            android:id="@+id/imageButtonGenerateSingle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="25dp"
            android:layout_marginBottom="40dp"
            android:layout_marginTop="0dp"
            android:layout_gravity="center_vertical"
            android:ellipsize="end"
            android:scrollHorizontally="true"
            android:singleLine="true"
            android:text="Generate Qr Code"
            android:textStyle="bold"
            android:layout_alignEnd="@+id/tv_question"
            android:layout_alignRight="@+id/tv_question"
            android:background="#00ffffff"
            android:src="@drawable/qr_code"
            android:layout_weight="1"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:layout_marginBottom="65dp"
            android:layout_marginTop="65dp"
            android:clickable="false"
            android:text="Generate" >
        </TextView>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:layout_marginBottom="45dp"
            android:layout_marginTop="85dp"
            android:clickable="false"
            android:text="Single" >
        </TextView>
    </FrameLayout>

    </LinearLayout>
</LinearLayout>
public class PlusOneFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public PlusOneFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment PlusOneFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static PlusOneFragment newInstance(String param1, String param2) {
        PlusOneFragment fragment = new PlusOneFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_plus_one, container, false);
        TextView tv= (TextView) view.findViewById(R.id.seat_serial_prefix);
        tv.setText("yourText");
        return view;
    }
}