自定义微调框对话框:对话框的弹出背景设置

时间:2016-03-03 15:15:38

标签: android android-spinner

我有一个旋转器,如下所示。虽然我可以设置行的样式(“raspberrypi-0”),但我无法为对话框的其余部分更改任何内容。

我想更改弹出背景颜色。如果可能,还应更改“为传感器选择设备”的颜色。下面是我在线性布局(未显示)中定义的微调器的xml。

<fr.ganfra.materialspinner.MaterialSpinner
    android:id="@+id/attached_device_value_dropdown"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:spinnerMode="dialog"
    app:ms_alignLabels="false"
    app:ms_arrowSize="@dimen/spinner_arrow_size"
    app:ms_hint="@string/attached_device_select"
    app:ms_enableErrorLabel="true"
    app:ms_multiline="true"
    app:ms_floatingLabelColor="@color/black"
    app:ms_hintColor="@color/light_grey"
    app:ms_baseColor="@color/black"
    app:ms_highlightColor="@color/black"
    app:ms_arrowColor="@color/black"/>

类似的设置:当将微调器模式设置为“下拉”时,存在popupBackground。我正在寻找与此类似的东西,但是要进行对话。

在Java中,这就是我设置我的微调器的方法:

CustomArrayAdapter customArrayAdapter = new CustomArrayAdapter(parentActivity, R.layout.spinner_dropdown_item, devices);
customArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
registeredDevicesDropdown.setAdapter(customArrayAdapter);

其中spinner_dropdown_item只是一个文字视图。

同样,MaterialSpinner只是扩展了Spinner,因此大部分内容都应该适用。 Here在github上有关于它的更多信息。

enter image description here

1 个答案:

答案 0 :(得分:1)

我通过黑客攻击MaterialSpinner的适配器代码解决了这个问题。在覆盖的getCustomView函数中,设置背景和文本的颜色。

public View getCustomView(int position, View convertView, ViewGroup parent, boolean isDropDownView) {
    //TODO: use convert view.
    LayoutInflater inflater = (LayoutInflater) getActivity()
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final int resid = isDropDownView ? android.R.layout.simple_spinner_dropdown_item : android.R.layout.simple_spinner_item;
    TextView row = (TextView) inflater.inflate(resid, parent, false);
    row.setText(devices.get(position).getDeviceLabel());

    //Hack: Cannot set the dialogs color from XML and text color from XML                                                          
    row.setTextColor(ContextCompat.getColor(parentActivity, R.color.black));        //set Text Color for the row content
    parent.setBackgroundColor(ContextCompat.getColor(parentActivity, R.color.colorSecondary));      //set backgraound color ofr skeleton.
    return row;
}
相关问题