片段不会显示在活动中

时间:2018-11-17 04:20:34

标签: java android android-fragments

我对android开发非常陌生。我浏览了一些有关片段的教程,但是由于某种原因,我似乎无法从主视图中添加片段。我没有尝试做任何痛苦的事情。我要做的只是创建片段并将其添加到我的主要活动中。而已。但是,每次运行应用程序时,我得到的都是空白屏幕。我对其进行调试,然后将其片段化。这使我相信问题可能出在xml laout文件中(也许)。

这是我的MainActivity.java

package com.android.myCompany.nameOfApp;

import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Begin the transaction
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.foo_frame_layout, new FooFragment());
        ft.commit();
    }
}

这是我的Fragment.cs

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * A simple {@link Fragment} subclass.
 */
public class FooFragment extends Fragment {

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


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        TextView textView = new TextView(getActivity());
        textView.setText(R.string.hello_blank_fragment);
        return textView;
    }
}

这是我的布局activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="@drawable/background_portrait">

    <FrameLayout
        android:id="@+id/copyright_frame_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>
</RelativeLayout>

这是我的fragment_foo.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/copyright_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".FooFragment">
<!-- TODO: Update blank fragment layout -->

    <ImageView
        android:id="@+id/imageView5"
        android:layout_width="wrap_content"
        android:layout_height="102dp"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="8dp"
        android:adjustViewBounds="false"
        android:cropToPadding="false"
        app:layout_constraintBottom_toTopOf="@+id/imageView6"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.504"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/logo" />

    <ImageView
</RelativeLayout>

有人可以告诉我我的设置有什么问题吗?预先非常感谢。

2 个答案:

答案 0 :(得分:2)

在您的片段类中对此进行更改:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_foo, container, false); // This line will inflate your fragment_foo layout and return it's rootview for fragment inflation
}

,然后在onViewCreated片段方法中找到片段视图。

答案 1 :(得分:0)

尝试将其添加到您的片段类中...

public static FooFragment newInstance()
{
    return new FooFragment();
}

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_layout, container, false);

    // work with your ui code here.
    return view;
}

然后在您的活动中调用此方法...

// Begin the transaction
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.foo_frame_layout, FooFragment.newInstance());
ft.commit();