在android中选择状态时页脚图像没有改变

时间:2013-06-27 11:30:15

标签: android android-layout

我已经为学习目的制作了一个简单的脚本菜单安卓程序,我想要的是当我选择页脚的4个菜单中的任何一个时,应该更改所选择的菜单图像。我试过如下,但是它不起作用我放了图片和问题也请检查一下:enter image description here

代码是:

menu.java P

ackage com.esp.therisemethod.uc;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.esp.therisemethod.R;
import com.esp.therisemethod.ui.ContactusActivity;
import com.esp.therisemethod.ui.InfoActivity;
import com.esp.therisemethod.ui.ProgressActivity;
import com.esp.therisemethod.ui.ProgressGraph;

public class Menu extends RelativeLayout implements OnClickListener {

    public LinearLayout llfooterHome;
    public LinearLayout llfooterGraph = null;
    public LinearLayout llfooterInfo = null;
    public LinearLayout llfooterContactUs = null;
    public Context objContext;

    public Menu(Context context) {
        super(context);
        objContext = context;
        init();
    }

    public Menu(final Context context, AttributeSet attrs) {
        super(context, attrs);
        objContext = context;
        init();
    }

    public void init() {
        View view = LayoutInflater.from(objContext).inflate(R.layout.uc_menu,
                this, true);

        llfooterHome = (LinearLayout)view.findViewById(R.id.llfooterHome);
        llfooterGraph = (LinearLayout) view.findViewById(R.id.llfooterGraph);
        llfooterInfo = (LinearLayout) view
                .findViewById(R.id.llfooterInfo);
        llfooterContactUs = (LinearLayout) view
                .findViewById(R.id.llfooterContactUs);

        llfooterGraph.setOnClickListener(this);
        llfooterInfo.setOnClickListener(this);
        llfooterContactUs.setOnClickListener(this);
        llfooterHome.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
        case R.id.llfooterHome:

            intent = new Intent(objContext, ProgressActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            objContext.startActivity(intent);
            break;
        case R.id.llfooterGraph:
            intent = new Intent(objContext, ProgressGraph.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            objContext.startActivity(intent);
            break;
        case R.id.llfooterInfo:
            intent = new Intent(objContext, InfoActivity.class);
            objContext.startActivity(intent);

            break;
        case R.id.llfooterContactUs:
            intent = new Intent(objContext, ContactusActivity.class);
            objContext.startActivity(intent);

            break;
        }

    }

    public void setSelectedTab(int selectTab) {
        switch (selectTab) {
        case 1:
            llfooterHome.setBackgroundResource(R.drawable.home_active);

            llfooterGraph.setBackgroundResource(R.drawable.graph_default);
            llfooterInfo.setBackgroundResource(R.drawable.info_default);
            llfooterContactUs.setBackgroundResource(R.drawable.contactus_default);
            break;
        case 2:
            llfooterHome.setBackgroundResource(R.drawable.home_default);

            llfooterGraph.setBackgroundResource(R.drawable.graph_active);
            llfooterInfo.setBackgroundResource(R.drawable.info_default);
            llfooterContactUs.setBackgroundResource(R.drawable.contactus_default);

            break;
        case 3:
            llfooterHome.setBackgroundResource(R.drawable.home_default);

            llfooterGraph.setBackgroundResource(R.drawable.graph_default);
            llfooterInfo.setBackgroundResource(R.drawable.info_active);
            llfooterContactUs.setBackgroundResource(R.drawable.contactus_default);
            break;

        case 4:
            llfooterHome.setBackgroundResource(R.drawable.home_default);

            llfooterGraph.setBackgroundResource(R.drawable.graph_default);
            llfooterInfo.setBackgroundResource(R.drawable.info_default);
            llfooterContactUs.setBackgroundResource(R.drawable.contactus_active);
            break;
        }
    }

}

1 个答案:

答案 0 :(得分:0)

这段代码对我有用,

switch (v.getId()) {
        case R.id.llfooterHome:

            intent = new Intent(objContext, ProgressActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            objContext.startActivity(intent);
            break;
        case R.id.llfooterGraph:
            intent = new Intent(objContext, ProgressGraph.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            objContext.startActivity(intent);
            break;
        case R.id.llfooterInfo:
            intent = new Intent(objContext, InfoActivity.class);
            objContext.startActivity(intent);

            break;
        case R.id.llfooterContactUs:
            intent = new Intent(objContext, ContactusActivity.class);
            objContext.startActivity(intent);

            break;
        }