如何在片段中设置setContentView

Nee*_*ava 0 android android-fragments setcontentview

我试图在一个片段中调用一个库,但是不知道如何在一个片段中设置它我已经在主要活动中完成了但是我在我的片段中设置setContentView时遇到错误编译依赖

compile 'com.github.medyo:android-about-page:1.0.2'
Run Code Online (Sandbox Code Playgroud)

我的片段内容视图

  @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView  = inflater.inflate(R.layout.fragment_navigation, container, false);
    Element versionElement = new Element();
    versionElement.setTitle("Version 6.2");

    Element adsElement = new Element();
    adsElement.setTitle("Advertise with us");

    View aboutPage = new AboutPage(getActivity())
            .isRTL(false)
            .addItem(versionElement)
            .addItem(adsElement)
            .addGroup("Connect with us")
            .addEmail("elmehdi.sakout@gmail.com")
            .addFacebook("the.medy")
            .addTwitter("medyo80")
            .addYoutube("UCdPQtdWIsg7_pi4mrRu46vA")
            .addPlayStore("com.ideashower.readitlater.pro")
            .addInstagram("medyo80")
            .addGitHub("medyo")
            .create();

    setContentView(aboutPage);
    return rootView;
}
Run Code Online (Sandbox Code Playgroud)

我在倒数第二行得到错误如何解决这个问题.以下库可在api 20+库https://github.com/medyo/android-about-page中使用

Jua*_*tés 6

在您没有setContentView明确调用的片段上,您可以在膨胀后返回视图,就像您一样.因此,不要setContentView考虑将视图添加aboutPagerootView其子视图或其中一个子视图.

例如,假设您的布局R.layout.fragment_navigation包含ID LinearLayout(或其他任何其他ViewGroup内容),其ID为content.你会在退货声明之前这样做:

LinearLayout content = (LinearLayout) rootView.findViewById(R.id.content);
content.addView(aboutPage); //<-- Instead of setContentView(aboutPage)
Run Code Online (Sandbox Code Playgroud)

你必须根据你的布局进行调整,我不知道里面有什么.

完整的例子

在此输入图像描述

fragment.xml之

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/container">
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

CustomFragment.java

public class FragmentExample extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment, container, false);
        Element versionElement = new Element();
        versionElement.setTitle("Version 6.2");

        Element adsElement = new Element();
        adsElement.setTitle("Advertise with us");

        View aboutPage = new AboutPage(getActivity())
                .isRTL(false)
                .addItem(versionElement)
                .addItem(adsElement)
                .addGroup("Connect with us")
                .addEmail("elmehdi.sakout@gmail.com")
                .addFacebook("the.medy")
                .addTwitter("medyo80")
                .addYoutube("UCdPQtdWIsg7_pi4mrRu46vA")
                .addPlayStore("com.ideashower.readitlater.pro")
                .addInstagram("medyo80")
                .addGitHub("medyo")
                .create();

        viewGroup.addView(aboutPage);
        return viewGroup;
    }
}
Run Code Online (Sandbox Code Playgroud)