如何在fragmenActivity类中初始化并引用imageButton

时间:2016-05-11 05:26:23

标签: java android-studio android-imagebutton

**我是android的新手,我试图使用findbyView来引用一个imageButton,但它一直说无法解析findbyView,我理解我只能在活动类中使用find来查看,还有什么方法可以请初始化一个imageButton **

2 个答案:

答案 0 :(得分:0)

根据您关于如何实例化图像按钮类的问题,我确定您必须已导入按钮类。如果是,那么这是推荐的做法

ImageButton btn = (ImageButton)findViewById(R.id.imageid);它始终有效

答案 1 :(得分:0)

覆盖FragmentActivity中的onCreateView(..)方法并使用充气器来膨胀你的视图并使用该视图来获取所有元素

 @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView=inflater.inflate(R.layout.fragment_home_layout, container, false);
            imageButton= (ImageButton) rootView.findViewById(R.id.imgbtnid);
}