我正在尝试在片段中调用库,但不知道如何在片段中设置它我已在主活动中完成它,但在片段中设置 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("[email protected] /cdn-cgi/l/email-protection")
.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 https://github.com/medyo/android-about-page
在你不调用的片段上setContentView
明确地,您在膨胀视图后返回视图,就像您一样。所以而不是打电话setContentView
考虑添加视图aboutPage
to rootView
或其子视图之一。
例如,说出您的布局R.layout.fragment_navigation
包含一个LinearLayout
(或任何其他ViewGroup
就此而言),其 ID 为content
。您可以在返回语句之前执行此操作:
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>
自定义Fragment.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("[email protected] /cdn-cgi/l/email-protection")
.addFacebook("the.medy")
.addTwitter("medyo80")
.addYoutube("UCdPQtdWIsg7_pi4mrRu46vA")
.addPlayStore("com.ideashower.readitlater.pro")
.addInstagram("medyo80")
.addGitHub("medyo")
.create();
viewGroup.addView(aboutPage);
return viewGroup;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)