0

我正在尝试在片段中调用库但不知道如何在片段中设置它我已经在主要活动中完成了但是在我的片段中设置 setContentView 编译依赖项时出现错误

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

4

4 回答 4

8

在您没有setContentView显式调用的片段上,您在膨胀后返回视图,就像您一样。因此,不要调用setContentView考虑将视图添加aboutPagerootView其子视图之一。

例如,假设您的布局包含R.layout.fragment_navigationID为. 您可以在您的退货声明之前执行此操作:LinearLayoutViewGroupcontent

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

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

完整示例

在此处输入图像描述

片段.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>

自定义片段.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;
    }
}
于 2016-04-26T16:05:52.440 回答
1

setContentView() 用于活动,对于片段,您必须在 onCreateView() 方法上返回膨胀的布局,如下所示:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.article_view, container, false);
}

我希望它有所帮助。

于 2016-04-26T20:54:47.517 回答
0

在 Android X 中,您只需先导入

import androidx.fragment.app.Fragment

然后在构造函数中传递你的布局ID,如R.layout.fragment_navigation,然后覆盖onViewCreated方法(它也更安全),而不是你只实现onCreateView()。例子:

import android.view.View
import android.widget.*
import androidx.fragment.app.Fragment

class MahrInCoinFragment : Fragment(R.layout.fragment_navigation) {

private lateinit var mCoin: Coin

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    // Load coin saved data or use default value
    val cost: Int = Hawk.get(Constant.COIN_COST_KEY, Constant.DEFAULT_COIN_PRICE)
    val date: String = Hawk.get(Constant.COIN_DATE_KEY, Constant.DEFAULT_COIN_DATE)
    updateCoin(Coin().apply { this.cost = cost; this.date = date })

    // Handle listeners
    view.findViewById<Button>(R.id.btn_calculate).setOnClickListener(this)
    view.findViewById<ImageButton>(R.id.btn_update).setOnClickListener(this)
}
于 2019-04-06T16:33:35.493 回答
0

如果您只需要 aboutPage 而不是将 return 语句更改为return aboutPage;

于 2016-04-26T16:31:31.330 回答