怎么在Activity中用Fragment的ImageView的控件
时间: 2024-05-06 12:19:52 浏览: 98
首先,在你的Activity布局文件中添加一个Fragment:
```
<fragment
android:id="@+id/myFragment"
android:name="com.example.MyFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
然后,在你的Fragment中添加一个ImageView控件:
```
public class MyFragment extends Fragment {
private ImageView mImageView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout, container, false);
mImageView = (ImageView) view.findViewById(R.id.myImageView);
return view;
}
public void setImageResource(int resId) {
mImageView.setImageResource(resId);
}
}
```
在Activity中,你可以通过以下方式获取Fragment,并调用其方法来设置ImageView的资源:
```
MyFragment myFragment = (MyFragment) getSupportFragmentManager().findFragmentById(R.id.myFragment);
myFragment.setImageResource(R.drawable.my_image);
```
阅读全文
相关推荐













