【问题标题】:LayoutInflater in ActivityActivity中的LayoutInflater
【发布时间】:2016-01-22 16:34:14
【问题描述】:

我需要 LayoutInflater 方面的帮助。 在我的项目中,我收到“避免将null 作为视图根传递(需要解析布局参数......”lint 警告。 当在 OnCreate 方法中我有这样的事情时,这个警告来自 Activity:

LayoutInflater inflater =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.my_view, null);

例如,它是为了膨胀一个标题。

我知道当我在 Fragment 或 Adapter 中使用 LayoutInflater 时,我有 ViewGroup 对象,我可以将它传递给 null,但是当它在 Activity 中时,在这种情况下我应该怎么做?我应该取消这个警告并传递 null 还是以某种方式创建父对象?

编辑:

public void addTextField(String message, int textSize) {
    LinearLayout field = (LinearLayout) getLayoutInflater().inflate(R.layout.text_view_field, null);
    TextView textView = (TextView) field.findViewById(R.id.taroTextView);
    textView.setText(message);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
    textView.setSingleLine(false);
    mFieldsLayout.addView(field, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}

或:

public class MyActionBarActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowTitleEnabled(false);
        LayoutInflater inflater = LayoutInflater.from(this);

        View customView = inflater.inflate(R.layout.action_bar_layout, null);
        actionBar.setCustomView(customView);
        actionBar.setDisplayShowCustomEnabled(true);
    }
}

}

【问题讨论】:

  • 你不能在你的代码中使用 findViewById 吗?但在使用 findViewById 获取代码处理程序之前,请先使用 setContentView 设置布局

标签: android android-activity layout-inflater


【解决方案1】:

你为什么不在你的活动中直接调用getLayoutInflater()

如下:

View view = getLayoutInflater().inflate(R.layout.my_view, null);

null 参数不是问题。它在我的活动中运行良好。

【讨论】:

    【解决方案2】:

    您应该传递将包含视图的父级。

    例如:

    ViewGroup container = (ViewGroup) findViewById(R.id.header_container);
    View view = getLayoutInflater().inflate(R.layout.my_view, container, false);
    container.addView(view);
    

    这只是必要的,因此您正在膨胀的视图保留它的父级依赖属性,例如边距。如果你通过null,这些将被设置为它们的默认值。

    【讨论】:

      【解决方案3】:

      这只是一个警告。您有时需要传递 null。如果是故意的,那也没什么不好。 android 框架中的许多小部件也这样做。

      如果你想抑制警告使用这个:

      @SuppressLint("InflateParams")
      

      关于您的声明/方法或类。

      例如:

      @SuppressLint("InflateParams")
      View view = inflater.inflate(R.layout.my_view, null);
      

       @SuppressLint("InflateParams")
      void yourMethod(){
      LayoutInflater inflater =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      View view = inflater.inflate(R.layout.my_view, null);
      }
      

      只要确保你在这里所做的事情是正确的。警告只是为了让您检查代码的正确性..

      【讨论】:

      • 你能给我举个例子,说明我应该在 Activity 中传递父级而不是 null 吗?
      • 通常用于自定义布局。你可以在这里阅读更多内容:possiblemobile.com/2013/05/layout-inflation-as-intended你对你膨胀的观点做了什么..
      • 它只是带有活动名称的标题。当 inflate 用于添加标题或在对话框中添加文本时,我有项目(总是在片段或适配器传递父项中始终使用 null)这是一个好方法吗?
      • 你能再分享几行代码吗?需要了解更多关于如何使用视图对象的信息……
      【解决方案4】:

      这个解决方案对我有用:

      View view = inflater.inflate(R.layout.my_view, (ViewGroup) getWindow().getDecorView(), false);
      

      这样,您的 View 就有一个未附加的根 (DecorView)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多