【问题标题】:how to set setContentView in fragment如何在片段中设置 setContentView
【发布时间】:2016-04-26 16:03:05
【问题描述】:

我正在尝试在片段中调用库,但不知道如何在片段中设置它我已在主要活动中完成,但在片段中设置 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

【问题讨论】:

    标签: android android-fragments setcontentview


    【解决方案1】:

    在你没有显式调用setContentView 的片段上,你在膨胀它之后返回视图,就像你一样。因此,不要调用setContentView,而是考虑将视图aboutPage 添加到rootView 或其子视图之一。

    例如,假设您的布局 R.layout.fragment_navigation 包含一个 ID 为 content 的 LinearLayout(或任何其他 ViewGroup)。您可以在您的退货声明之前执行此操作:

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

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

    完整示例

    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>
    

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

    【讨论】:

    • 但它是在库中给出的(我是 android 新手)所以请指导我
    • 在添加根视图或子视图时仍然报错
    • 试过这种方式 aboutPage.addView(aboutPage);或rootView
    • 不要自己添加!
    • 你下面的例子展示了如何在活动中做。在片段中,方法 onCreateView 必须膨胀并返回您要显示的视图。
    【解决方案2】:

    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】:

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

      【讨论】:

        【解决方案4】:

        在 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)
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-07-30
          • 2023-03-18
          • 1970-01-01
          相关资源
          最近更新 更多