2016-03-03 154 views
1

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

我想改变弹出的背景颜色。如果可能的话,“选择传感器的设备”的颜色也应该改变。下面是我在线性布局内定义的微调器的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

我通过黑客的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; 
}