0

i have a relative layout in navigation drawer. and it includes a textview and an imageview. i set onClick attribute="onMenuHome" tu relative layout

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();
    }

so background of relative layout and textcolor of textview changes. but it cant change imageview's src. can anyone help me?

4

1 回答 1

0

You need to stick that View with which you are dealing..

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);

Your Complete answer is:

    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();
}
于 2015-01-21T05:48:38.510 回答