English 中文(简体)
直接调用布局充气器和不直接调用布局充气器有什么区别?
原标题:What is the difference between calling LayoutInflater directly and not?

我看过一些教程, 在Android Doc中, 它说在即时不能直接访问布局充气器。 例如谷歌 Doc:

LayoutInflater inflater = (LayoutInflater)context.getSystemService
  (Context.LAYOUT_INFLATER_SERVICE);

我所经历的教程就是这个:

LayoutInflater inflater = LayoutInflater.from(parent.getContext());

所以,我真正不明白的是,除了明显的不同的代码外,区别是什么。任何解释都非常感激。我认为安道尔德医生应该是我们追随的医生,但我不知道这是否有什么不同。

最佳回答

如果您打开了 Android 源头, 您可以看到布局充气器。 从方法上看, 看起来像 :

/**
 * Obtains the LayoutInflater from the given context.
 */
public static LayoutInflater from(Context context) {
    LayoutInflater LayoutInflater =
            (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (LayoutInflater == null) {
        throw new AssertionError("LayoutInflater not found.");
    }
    return LayoutInflater;
}

这意味着您问题中的两行代码也做同样的事情。 不知道您所读的教程究竟写了什么, 但我看不出在功能上有什么区别。 使用 < code> from 方法的 < code > 方法是不错的, 因为它需要少一点打字, 就是这样 。

问题回答
LayoutInflater inflater = (LayoutInflater)context.getSystemService
  (Context.LAYOUT_INFLATER_SERVICE);

您正在从 System 管理器 获得 LayootInbiter 服务供应商 < /code> system 管理器

LayoutInflater inflater = LayoutInflater.from(parent.getContext());

您正在使用 stistic 方法,来自 LayooutIndibiter 类

我要说,区别只在于代码, 以及您如何写这个也称为堆叠但结果是一样的 - 你会得到LayoutInpitriter

更多有关"的更多信息 http:// developmenter.android.com/reference/android/croid/view/LayoutInbitler.html" rel="无跟随" > this

观 观 观





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...