如何删除带有片段 android 的 activity 中的图像视图
how to remove imageview in an activity with fragment android
这是我的片段代码如下:
此片段将在 MainActivity
中调用
public class StylePreviewFragments extends Fragment{
private CustomImageView ivPhoto;
Intent intent;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view=inflater.inflate(R.layout.choosephoto,container,false);
initialiseViews(view);
return view;
}
private void initialiseViews(View view) {
ivPhoto = (CustomImageView) view.findViewById(R.id.ivGalleryPhoto);
ivPhoto.setImageURI(Uri.fromFile(new File(ShowCapturedPictureActivity.path)));
}
}
主要活动代码:
它包含我必须用片段替换的图像视图。添加了自定义 id
的图像视图
public class ShowCapturedPictureActivity extends FragmentActivity{
private static final int CONTENT_VIEW_ID = 10101010;
StylePreviewFragments stylepreviewfragmnets;
public static String path;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.picturepreviiew);
iv_showImage=(ImageView) findViewById(R.id.iv_ShowImage);
intent=getIntent();
path=intent.getExtras().get("imagepath").toString();
// adding cutom id to imageview
iv_showImage.setId(CONTENT_VIEW_ID);
callfragment();// calling fragment
}
public void callfragment() // replacing imageview with fragments
{
stylepreviewfragmnets = new StylePreviewFragments();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(CONTENT_VIEW_ID,stylepreviewfragmnets);
ft.commit();
}
}
你可以这样做:
- 将 yourImageView
ImageView
放入 FrameLayout
yourContainer
- 在您的
callfragment()
中,设置您的 ImageView 可见性 View.GONE
,然后将 stylepreviewfragmnets
添加到 FrameLayout
您的容器
希望对您有所帮助!
抱歉,我将片段放入视图中的做法不正确,我应该放入视图组中。所以我使 imageview 成为线性布局的子级,然后我用该线性布局替换片段。它就像一个魅力。
ft.add(linearlayout.getId(),stylepreviewfragmnets);
ft.commit();
这是我的片段代码如下: 此片段将在 MainActivity
中调用 public class StylePreviewFragments extends Fragment{
private CustomImageView ivPhoto;
Intent intent;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view=inflater.inflate(R.layout.choosephoto,container,false);
initialiseViews(view);
return view;
}
private void initialiseViews(View view) {
ivPhoto = (CustomImageView) view.findViewById(R.id.ivGalleryPhoto);
ivPhoto.setImageURI(Uri.fromFile(new File(ShowCapturedPictureActivity.path)));
}
}
主要活动代码: 它包含我必须用片段替换的图像视图。添加了自定义 id
的图像视图public class ShowCapturedPictureActivity extends FragmentActivity{
private static final int CONTENT_VIEW_ID = 10101010;
StylePreviewFragments stylepreviewfragmnets;
public static String path;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.picturepreviiew);
iv_showImage=(ImageView) findViewById(R.id.iv_ShowImage);
intent=getIntent();
path=intent.getExtras().get("imagepath").toString();
// adding cutom id to imageview
iv_showImage.setId(CONTENT_VIEW_ID);
callfragment();// calling fragment
}
public void callfragment() // replacing imageview with fragments
{
stylepreviewfragmnets = new StylePreviewFragments();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(CONTENT_VIEW_ID,stylepreviewfragmnets);
ft.commit();
}
}
你可以这样做:
- 将 yourImageView
ImageView
放入FrameLayout
yourContainer - 在您的
callfragment()
中,设置您的 ImageView 可见性View.GONE
,然后将stylepreviewfragmnets
添加到FrameLayout
您的容器
希望对您有所帮助!
抱歉,我将片段放入视图中的做法不正确,我应该放入视图组中。所以我使 imageview 成为线性布局的子级,然后我用该线性布局替换片段。它就像一个魅力。
ft.add(linearlayout.getId(),stylepreviewfragmnets);
ft.commit();