我在导航抽屉里有一个相对布局。它包括textview和imageview。我设置了onClick attribute =" onMenuHome"相对布局
public void onMenuHome(View v) {
RelativeLayout rlayHome = (RelativeLayout) findViewById(R.id.rlay_main_home);
ImageView imgvHome = (ImageView) findViewById(R.id.imgv_main_home);
TextView txtvHome = (TextView) findViewById(R.id.txtv_main_home);
rlayHome.setBackgroundResource(R.color.clr_disabledLight);
imgvHome.setBackgroundResource(R.drawable.ic_home_c);
txtvHome.setTextColor(getResources().getColor(R.color.clr_primaryColor));
Fragment fragment = new HomeFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frml_main_fragment, fragment).commit();
mTitle = getResources().getString(R.string.str_home);
getSupportActionBar().setTitle(mTitle);
mDrawerLayout.closeDrawers();
}
因此textview的相对布局和textcolor的背景会发生变化。但是它无法改变imageview的src。 有谁可以帮助我?
答案 0 :(得分:0)
您需要坚持使用您正在处理的视图..
RelativeLayout rl =(RelativeLayout) findViewById(R.id.rlay_main_home_);
// then find views components (i.e buttons, textviews) via above view
Button btn = (Button) rl.findViewById(R.id.btn_id);
您的完整答案是:
public void onMenuHome(View v) {
RelativeLayout rlayHome = (RelativeLayout) findViewById(R.id.rlay_main_home);
ImageView imgvHome = (ImageView)rlayHome .findViewById(R.id.imgv_main_home);
TextView txtvHome = (TextView)rlayHome .findViewById(R.id.txtv_main_home);
rlayHome.setBackgroundResource(R.color.clr_disabledLight);
imgvHome.setBackgroundResource(R.drawable.ic_home_c);
txtvHome.setTextColor(getResources().getColor(R.color.clr_primaryColor));
Fragment fragment = new HomeFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frml_main_fragment, fragment).commit();
mTitle = getResources().getString(R.string.str_home);
getSupportActionBar().setTitle(mTitle);
mDrawerLayout.closeDrawers();
}