首页 文章

从Activity访问android自定义ActionBar

提问于
浏览
0

我创建了一个扩展ActionBarActivity并显示自定义XML的类 . 几乎所有的活动都扩展了这门课程 .

我想从我的一个活动中访问该自定义XML的元素 . 假设我想在Activity2中更改item2的背景 .

在我的activity的onCreate方法中,在setContentView之后,我尝试了:

View cView = getLayoutInflater().inflate(R.layout.custom_menu, null);
ImageButton rewards_link = (ImageButton) cView.findViewById(R.id.rewards_link);
rewards_link.setVisibility(View.GONE); // For test purpose

即使按钮ID似乎正确,更改也不适用 . 有任何想法吗 ?

1 回答

  • 0

    如果您通过 getActionBar().setCustomView(R.layout.custom_menu); (或 getSupportActionBar() for AppCompat的v21)设置自定义视图,则可以直接使用 findViewById() 来访问这些视图,因为视图是视图层次结构的一部分,就像通过 setContentView() 添加的视图一样:

    ImageButton rewards_link = (ImageButton) findViewById(R.id.rewards_link);
    rewards_link.setVisibility(View.GONE); // For test purpose
    

相关问题