如何在片段中设置setContentView

时间:2016-04-26 16:03:06

标签: android android-fragments setcontentview

我试图在一个片段中调用一个库,但是不知道如何在一个片段中设置它我已经在主要活动中完成了它但是我在片段中设置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 个答案:

答案 0 :(得分:5)

在片段上,您没有明确地调用setContentView,您可以在充气后返回视图,就像您一样。因此,请考虑将视图setContentView添加到aboutPage或其中一个子视图,而不是调用rootView

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

content

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

完整示例

enter image description here

fragment.xml之

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

CustomFragment.java

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

setContentView()用于活动,对于Fragments,你必须在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);
}

我希望它有所帮助。

答案 3 :(得分: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)
}