包括布局android - 导致按钮背景颜色的问题

时间:2014-03-31 04:27:53

标签: android android-layout android-fragments android-button

我有一个布局,这在我项目的所有3个标签(片段)中很常见。所以我所做的是为共同结构创建了单独的布局,并在所有片段布局中包含了布局。布局基本上有4个按钮,点击这些按钮我会改变按钮的背景颜色并相应地显示数据。

This is the 1st tab in which its coming perfect.

This is the second tab where all the buttons are getting the blue color.

代码: -

@Override
    public View onCreateView(final LayoutInflater inflater,
            final ViewGroup container, final Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.fragment,
                container, false);
        mLvIncomingCalls = (ListView) rootView.findViewById(R.id.lv);

        mBtnDay = (Button) rootView.findViewById(R.id.btnDay);
        mBtnWeek = (Button) rootView.findViewById(R.id.btnWeek);
        mBtnMonth = (Button) rootView.findViewById(R.id.btnMonth);
        mBtnAllTime = (Button) rootView.findViewById(R.id.btnAllTime);

        mBtnDay.setOnClickListener(btnDayClickListener);
        mBtnWeek.setOnClickListener(btnWeekClickListener);
        mBtnMonth.setOnClickListener(btnMonthClickListener);
        mBtnAllTime.setOnClickListener(btnAllTimeClickListener);

        mBtnDay.setBackgroundColor(getResources().getColor(
                android.R.color.holo_blue_light));

        return rootView;
    }

现在正如快照中所见,问题是在第二个标签中默认情况下所有按钮都会获得蓝色。我不知道为什么它为所有按钮变蓝,因为在我添加上述代码的所有片段中默认情况下只将Day按钮颜色设置为蓝色。

我是否需要为所有人创建单独的布局?

任何人都有任何想法,请指导我。

2 个答案:

答案 0 :(得分:1)

放上onTabChangedListener

mTabHost.setOnTabChangedListener(new OnTabChangeListener() {

        @Override
        public void onTabChanged(String tabId) {
            // TODO Auto-generated method stub
            if (tabId.equals("first")) {
                // here you can set "setBackgroundColor"
            }else if (tabId.equals("second")) {
            ......
            ......
        }
    }
}

我认为这会有所帮助。
谢谢。

答案 1 :(得分:1)

onCreateView中,您可以将其他按钮颜色设置为灰色。这种方式肯定不会发生。它发生的原因是个谜,我相信系统会以某种方式错误地回收Drawable

相关问题