![](/img/trans.png)
[英]How to access the view of ViewPager of the fragment in the Activity?
[英]How to access image view of an activity from fragment?
我有一個帶有工具欄的活動,在該工具欄中,我添加了一個圖像視圖並在其中設置了徽標。
我還創建了不同的片段,現在當我看到片段時,我想隱藏該圖像視圖並將標題設置為活動的工具欄。
我已經將圖像視圖和工具欄設置為家庭活動中的public static。
我試圖像這樣訪問工具欄:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_account, container, false);
final Toolbar toolbar = (Toolbar) ((HomeActivity) getActivity()).findViewById(R.id.toolbar);
toolbar.setTitle(R.string.menu_account);
((HomeActivity) getActivity()).setSupportActionBar(toolbar);
((HomeActivity) getActivity()).mLogo.setVisibility(View.GONE);
return view;
}
但是我在圖像視圖上得到了一個空指針,如果注釋了setVisibility(View.GONE),則可以在工具欄上看到圖像。
家庭活動
public class HomeActivity extends AppCompatActivity{
public static Toolbar toolbar;
public static ImageView mLogo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mLogo = (ImageView)findViewById(R.id.imageViewLogo);
FragmentManager fragmentManager = HomeActivity.this.getFragmentManager();
MainFragment fragment = new MainFragment();
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
fragmentManager.beginTransaction().replace(R.id.mycontainer, fragment).commitAllowingStateLoss();
}
}
請幫忙。 謝謝。
像這樣從Activity獲取Imageview
ImageView mLogo = (ImageView)getActivity().findViewById(R.id.imageViewLogo);
mLogo.setVisibility(View.GONE);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.