【问题标题】:Inflating a xml layout in a custom View class在自定义 View 类中膨胀 xml 布局
【发布时间】:2012-06-17 06:08:07
【问题描述】:

我有一个扩展View 的类View1。我想在这个类View1 中膨胀R.layout.test2.xml。我在这个类中放了以下代码

public class View1 extends View {

    View view;
    String[] countries = new String[] {"India", "USA", "Canada"};

    public View1( Context context) {
        super(context);
        LayoutInflater  mInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view=mInflater.inflate(R.layout.test2, null, false);
    }
}

从另一个类Home 我希望这个膨胀的视图在某些情况下存在,在Home 类中我编写了以下代码:

public class Home extends Activity{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
        CreateView();   
    }

    public void CreateView() {
        LinearLayout lv=(LinearLayout)findViewById(R.id.linearlayout);
        View1 view = new View1(Home.this);
        lv.addView(view);
    }
}

但是当我运行我的项目时,活动并没有显示任何东西。

【问题讨论】:

    标签: android view android-inflate


    【解决方案1】:

    您不能向View 类添加视图,而应使用ViewGroup 或其子类之一(如LinearlayoutRelativeLayout 等)。那么你的代码会是这样的:

        public class View1 extends LinearLayout {
    
            View view;
            String[] countries = new String[] {"India", "USA", "Canada"};
    
            public View1( Context context) {
                super(context);
                inflate(context, R.layout.test2, this);
            }
        }
    

    【讨论】:

    • 你能告诉我们为什么它没有通过扩展 View 来提供任何东西吗?
    • @Kabir121 View 类不支持向其添加其他视图,View 类代表单个个体 View(例如,如果你想构建一个圆形的 Button ) 而不是一个组(就像你膨胀那个布局文件时的情况一样)。
    • 这个解决方案应该可以工作,但这也意味着会有总是有一个视图的视图组,这总是会导致布局阶段的工作量增加。我可以说构建器范例会更好,但是你会错过扩展视图的全部意义。
    【解决方案2】:

    在 kotlin 中
    LayoutInflater.from(this).inflate(R.layout.custom_toolbar, null, false)

    【讨论】:

      【解决方案3】:

      使用下面的代码来扩展您的布局,然后您可以将该视图用于任何目的。这将为您提供 XML 文件的最父布局。键入 cast 并相应地使用它。

      View headerView = View.inflate(this, R.layout.layout_name, null);
      

      【讨论】:

      • 如果我在我的 View1 类中有一个列表视图,然后 onlistitem 单击我正在尝试扩充新视图,但它没有扩充新视图..请帮助某人
      • 您的列表项在自定义适配器或活动中单击的位置在哪里。
      【解决方案4】:

      你必须使用ViewGroupFrameLayout 并这样做:

      public class View1 extends FrameLayout {
      
          public View1(Context context) {
              super(context);
              inflate(context, R.layout.view1, this);
          }
      }
      

      在布局 XML 中,使用 <merge 标记不仅仅是将您的 view1 布局添加到根布局,这意味着我们有一个空的 FrameLayout 和您定义的视图并排在一起。

      <?xml version="1.0" encoding="utf-8"?>
      <merge xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
      
          <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center"
              android:text="content" />
      
      </merge>
      

      http://trickyandroid.com/protip-inflating-layout-for-your-custom-view/

      【讨论】:

        【解决方案5】:

        使用这个

            LayoutInflater li = (LayoutInflater)getContext().getSystemService(infService);
            li.inflate(R.layout.test2, **this**, true);
        

        您必须使用this,而不是null,并将false参数(布尔AttachToRoot)更改为true

        【讨论】:

        • 为此,您需要从 ViewGroup 子类化,而不是 View
        【解决方案6】:

        您正在添加家庭活动空白视图。因为在 View1 类中,您只有膨胀视图,但不能在任何地方添加。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-10-18
          • 1970-01-01
          相关资源
          最近更新 更多