如何在片段中设置setContentView [英] how to set setContentView in fragment

查看:228
本文介绍了如何在片段中设置setContentView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to call a library in a fragment but dont know how to set it in a fragment I have done it in the main activity but I am getting an error in setting the setContentView in my fragment the compile dependency

compile 'com.github.medyo:android-about-page:1.0.2'

我的片段内容视图

  @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;
}

我在倒数第二行中遇到了如何解决此问题的错误. 以下库将在api 20+中运行 库 https://github.com/medyo/android-about-page

I am getting error in the second last line how to solve this. The following library will work in api 20+ library https://github.com/medyo/android-about-page

推荐答案

在不显式调用setContentView的片段上,将视图放大后照原样返回.因此,与其调用setContentView,不如考虑将视图aboutPage添加到rootView或其子视图之一.

On a fragment you don't call setContentView explicitly, you return the view after inflating it, as you are. So instead of calling setContentView consider adding the view aboutPage to rootView or one of its children views.

例如,假设您的布局R.layout.fragment_navigation包含ID为contentLinearLayout(或与此有关的任何其他ViewGroup).您可以在返回语句之前执行此操作:

For example, say your layout R.layout.fragment_navigation contains a LinearLayout (or any other ViewGroup for that matter) with an ID of content. You would do this, before your return statement:

LinearLayout content = (LinearLayout) rootView.findViewById(R.id.content);
content.addView(aboutPage); //<-- Instead of setContentView(aboutPage)

您必须将其调整为您的布局,我不知道其中的内容.

You'll have to adjust this to your layout, I don't know what's inside it.

<?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>

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;
    }
}

这篇关于如何在片段中设置setContentView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆