【问题标题】:Access components within my custom view (inflated layout)?在我的自定义视图(膨胀布局)中访问组件?
【发布时间】:2011-11-14 18:06:38
【问题描述】:

我可能犯了一些愚蠢的错误。

我有一个自定义视图并使用 LayoutInflater 从 XML 获取布局。 现在,假设我有一个名为 bt1 的按钮。通常,我会使用 findViewById ,但这不起作用。

如何在该自定义视图中获取按钮?

LinearLayout lytContainer;
public obj(Context c){
    super(c);

    lytContainer = (LinearLayout) View.inflate(
            this.getContext(), R.layout.myLayout, null);
    TextView t = (TextView)findViewById(R.id.bt1);
    t.setText("cake");

}

【问题讨论】:

    标签: java android custom-view layout-inflater


    【解决方案1】:

    你必须使用:

        TextView t = (TextView)lytContainer.findViewById(R.id.bt1);
    

    【讨论】:

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