我在理解为什么style.xml
中的主题没有得到一致应用时遇到了问题。我将RadioButtons以编程方式放在一个片段中。 RadioGroup以XML格式定义。无论我在style.xml
中设置了什么,我选择的按钮都有一个黑色的中心。如果我创建一个新项目,在常规活动中以XML格式定义组和两个按钮,并应用相同的主题,那么所选按钮就是我设置的colorAccent
而不是黑色。
以下是以编程方式构建我的RadioGroup的代码
public class MainDataSelectionFragrment extends Fragment {
private static final String LOG_TAG = "MainDataSelectionFragrment";
private OnFragmentInteractionListener mCallback;
private TextView welcome;
private Spinner schoolList;
private String schoolCode = "";
private RadioGroup terms;
private String termSelected = "";
private Spinner dataTypes;
private String dataTypeSelected = "";
private Spinner subjects;
private String subjectSelected = "";
private Button search;
private TermsAsyncTask termsAsyncTask;
public MainDataSelectionFragrment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// track this view so that we can find views inside it
View rootView = inflater.inflate(R.layout.fragment_main_data_selection, container, false);
// views inside this one that will be modified in some way
this.welcome = (TextView) rootView.findViewById(R.id.textview_welcome);
this.schoolList = (Spinner) rootView.findViewById(R.id.spinner_school_list);
this.terms = (RadioGroup) rootView.findViewById(R.id.rg_terms);
this.dataTypes = (Spinner) rootView.findViewById(R.id.spinner_data_type);
this.subjects = (Spinner) rootView.findViewById(R.id.spinner_subjects);
this.search = (Button) rootView.findViewById(R.id.searchButton);
// assign listeners
this.onItemSelectedListenerForSchoolsSpinner();
this.setOnCheckedChangeListenerForTerm();
this.onItemSelectedListenerForDataTypesSpinner();
this.voidOnItemSelectedListenerForSubjectsSpinner();
this.onClickListenerForSearchButton();
// Register with the bus
OttoBus.getInstance().register(this);
// return this view
return rootView;
}
private void buildTermsRadioGroup(ArrayList<String> terms) {
// create new buttons and add them to the group
for (String term : terms) {
Log.i(LOG_TAG, "Creating RadioButton for " + term);
RadioButton radioButton = new RadioButton(getActivity());
radioButton.setText(StringUtils.capitalize(term));
radioButton.setLayoutParams(new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.MATCH_PARENT,
RadioGroup.LayoutParams.WRAP_CONTENT, 1f));
this.terms.addView(radioButton);
}
}
}
编辑1 回应评论:是的,我正在使用AppCompat-v7
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- customize the color palette -->
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">@color/colorPrimary</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<!-- colorAccent is used as the default value for colorControlActivated,
which is used to tint widgets -->
<item name="colorAccent">@color/colorAccent</item>
<!-- You can also set colorControlNormal, colorControlActivated
colorControlHighlight, and colorSwitchThumbNormal. -->
</style>
<!-- ActionBar / ToolBar styles -->
<style name="AppBar" parent="AppTheme">
<!-- android:textColorPrimary is the color of the title text
in the Toolbar, in the Theme.AppCompat theme: -->
<item name="android:textColorPrimary">@color/bar_textColorPrimary</item>
<!-- android:textColorPrimaryInverse is the color of the title
text in the Toolbar, in the Theme.AppCompat.Light theme: -->
<item name="android:textColorPrimaryInverse">@color/bar_textColorPrimaryInverse</item>
<item name="actionMenuTextColor">@color/bar_textColorPrimary</item>
<!-- android:textColorSecondary is the color of the menu
overflow icon (three vertical dots) -->
<item name="android:textColorSecondary">@color/bar_textColorPrimary</item>
</style>
</resources>