android– 将活动转换为片段

android– 将活动转换为片段,第1张

概述我正在将我的Activity转换为片段.我需要帮助 *** 作我的Activity代码作为片段运行.我知道我必须将onCreate()方法转换为onCreateView().我不知道该怎么做.我的onCreate()代码……@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(sa

我正在将我的Activity转换为片段.
我需要帮助 *** 作我的Activity代码作为片段运行.

我知道我必须将onCreate()方法转换为onCreateVIEw().
我不知道该怎么做.

我的onCreate()代码……

 @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_author_fact);        //to eget reference to TextVIEw for the fact        mAuthorFactTextVIEw = (TextVIEw) findVIEwByID(R.ID.authorFactTextVIEw);    //Extract the resource ID for the fact from the intent    //If none is provIDed, display the "fact missing" message    int authorFact = getIntent().getIntExtra(QuoteFragment.EXTRA_AUTHOR_FACT,            R.string.fact_error);    // put the fact string in the fact TextVIEw    mAuthorFactTextVIEw.setText(authorFact);    Toolbar toolbar = (Toolbar) findVIEwByID(R.ID.toolbar);    setSupportActionbar(toolbar);    floatingActionbutton fab = (floatingActionbutton) findVIEwByID(R.ID.fab);    fab.setonClickListener(new VIEw.OnClickListener() {        @OverrIDe        public voID onClick(VIEw vIEw) {            Snackbar.make(vIEw, "Replace with your own action", Snackbar.LENGTH_LONG)                    .setAction("Action", null).show();        }    });}

我需要使用这个代码结构

   @OverrIDe    public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container, Bundle savedInstanceState){        VIEw v = inflater.inflate(R.layout.fragment_BUG_List, container, false);        //Setup floating action button to add a new BUG        final VIEw addbutton = v.findVIEwByID(R.ID.add_button);    addbutton.setonClickListener(new VIEw.OnClickListener(){        @OverrIDe    public voID onClick(VIEw vIEw){            addBUG();        }    });    return v;}

我该怎么做呢?
我尝试了不同的东西,但我无法弄清楚.

解决方法:

简要解决问题

I need to change from activity to fragment

让这成为我们想要转换的布局.只是一个带有居中TextVIEw的简单relativeLayout.将活动转换为片段时,可以使用完全相同的布局.我把它命名为fragment_layout.xml.

当然,您稍后需要更改Activity的布局以包含Fragment,但这不是问题……

<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">    <TextVIEw            androID:ID="@+ID/textVIEw"            androID:text="Hello World"            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:layout_centerVertical="true"            androID:layout_centerHorizontal="true"/></relativeLayout>

这是我们要转换的活动.请注意,setContentVIEw加载了fragment_layout.xml文件,并使用findVIEwByID获取TextVIEw.

import androID.os.Bundle;import androID.support.v7.app.AppCompatActivity;import androID.Widget.TextVIEw;public class MainActivity extends AppCompatActivity {    private TextVIEw textVIEw;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.fragment_layout);        textVIEw = (TextVIEw) findVIEwByID(R.ID.textVIEw);    }}

这里的Fragment将与上面的Activity完全相同.请注意,现在使用inflate.inflate和fragment_layout.xml文件来获取VIEw,以便使用rootVIEw.findVIEwByID获取TextVIEw.

并且OnCreateVIEw需要从inflater返回该视图.

import androID.os.Bundle;import androID.support.v4.app.Fragment;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.TextVIEw;public class MainFragment extends Fragment {    private TextVIEw textVIEw;    @OverrIDe    public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container, Bundle savedInstanceState) {        VIEw rootVIEw = inflater.inflate(R.layout.fragment_layout, container, false);        textVIEw = (TextVIEw) rootVIEw.findVIEwByID(R.ID.textVIEw);        return rootVIEw;    }}
总结

以上是内存溢出为你收集整理的android – 将活动转换为片段全部内容,希望文章能够帮你解决android – 将活动转换为片段所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1100843.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存