在Android Fragment中扩充布局

时间:2018-06-06 07:17:48

标签: android android-fragments

我的代码库我看到两种方法来扩展片段中的布局,有什么区别?有最好的做法吗?

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    return View.inflate(getActivity(), R.layout.mylayout, null);
}

或者:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    return inflater.inflate(R.layout.mylayout, container, false);
}

3 个答案:

答案 0 :(得分:1)

请参阅<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.css" rel="stylesheet" /> <div class="fixed-top bg-light"> <div> <noscript> <h1 style="color:#FF0000;"> This reportFile will not be navigable without Javascript, please enabled Javascript </h1> </noscript> <img srcset="style/songkongheader.png 400w,style/songkongheader-medium.png 200w,style/songkongheader-small.png 100w," sizes="(max-width:800px) 200px,(max-width:600px) 100px,400px" class="mb-2"> </div> <header> <h2 class="subheading" title="Start Fixing Songs"> <a class="mx-2"> Fix Songs </a> </h2> </header> </div> <main class="container-fluid bg-primary bd-"> <div class="row"> <div class="col py-5">Hello</div> </div> </main>的来源:

View.inflate()

因此,在内部,public static View inflate(Context context, int resource, ViewGroup root) { LayoutInflater factory = LayoutInflater.from(context); return factory.inflate(resource, root); } 类的inflate()方法使用View,这使我认为没有区别。

Ref

答案 1 :(得分:0)

如果你研究第一个方法View.inflate(getActivity(),R.layout.mylayout,null); 您将看到它委托第二个inflater.inflate(R.layout.mylayout,container,false); 所以这些之间没有区别。

答案 2 :(得分:0)

它们具有完全相同的含义,并且在运行时间上没有差异