相关疑难解决方法(0)

片段中的findViewById

我试图在片段中创建一个ImageView,它将引用我在片段的XML中创建的ImageView元素.但是,该findViewById方法仅在扩展Activity类时才有效.无论如何,我还可以在片段中使用它吗?

public class TestClass extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ImageView imageView = (ImageView)findViewById(R.id.my_image);
        return inflater.inflate(R.layout.testclassfragment, container, false);
    }
}
Run Code Online (Sandbox Code Playgroud)

findViewById方法有一个错误,表明该方法是未定义的.

android android-imageview android-fragments findviewbyid

975
推荐指数
23
解决办法
56万
查看次数