我无法在Android中更改片段的背景颜色

时间:2017-03-09 05:17:08

标签: android android-fragments fragment background-color

我想更改片段的背景颜色。但是当我点击按钮时没有任何反应。

在我的主要活动布局XML文件中,我导入了片段。

这是我的代码:

$xAxisTickValues1 = array(
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$A$5', NULL, 4),  //  Q1 to Q4
);

2 个答案:

答案 0 :(得分:1)

您的代码非常完美,但您需要添加button.setOnClickListener(this);

     @Override
     public void onActivityCreated(Bundle savedInstanceState) {
     super.onActivityCreated(savedInstanceState);

     button = (Button)getActivity().findViewById(R.id.button);
     mLinearLayout = (LinearLayout)getActivity().findViewById(R.id.layout);
     button.setOnClickListener(this); }

答案 1 :(得分:0)

替换

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.top_fragment,container, false);

}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    button = (Button)getActivity().findViewById(R.id.button);
    mLinearLayout = (LinearLayout)getActivity().findViewById(R.id.layout);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View fragmentView = inflater.inflate(R.layout.top_fragment,container, false)

    button = (Button)fragmentView.findViewById(R.id.button);
    button.setOnClickListener(this);
    mLinearLayout = (LinearLayout)fragmentView.findViewById(R.id.layout);

    return fragmentView;

}