我正在尝试创建一个Spinner首选项,就像那个 http://www.hidroh.com/2015/11/30/building-custom-preferences-v7/
到目前为止,我已经像这样扩展了偏好:
routes.php
然而,这导致以下结果:
正如您所见," Server"尽管我没有触及它的风格,但标题看起来与其他偏好设置不同。填充也是非常不同的。为什么会发生这种情况?如何修改此样式?
此外,在同一图片中,您可以看到一个图标,是否可以将此图标对齐到顶部?
偏好XML:
public class SpinnerPreference extends Preference {
protected String[] mEntries = new String[0];
protected String[] mEntryValues = new String[0];
private int mSelection = 0;
private final LayoutInflater mLayoutInflater;
public SpinnerPreference(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SpinnerPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setWidgetLayoutResource(R.layout.spinner_preference);
mLayoutInflater = LayoutInflater.from(getContext());
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SpinnerPreference);
int entriesResId = ta.getResourceId(R.styleable.SpinnerPreference_entries, 0);
if (entriesResId != 0) {
mEntries = context.getResources().getStringArray(entriesResId);
}
int valuesResId = ta.getResourceId(R.styleable.SpinnerPreference_entryValues, 0);
if (valuesResId != 0) {
mEntryValues = context.getResources().getStringArray(valuesResId);
}
ta.recycle();
}
@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return a.getString(index);
}
@Override
protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
super.onSetInitialValue(restorePersistedValue, defaultValue);
String value = restorePersistedValue ? getPersistedString(null) : (String) defaultValue;
for (int i = 0; i < mEntryValues.length; i++) {
if (TextUtils.equals(mEntryValues[i], value)) {
mSelection = i;
break;
}
}
}
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
final Spinner spinner = (Spinner) holder.findViewById(R.id.spinner);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
spinner.performClick();
}
});
spinner.setAdapter(new SpinnerAdapter() {
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = createDropDownView(position, parent);
}
bindDropDownView(position, convertView);
return convertView;
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
// no op
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
// no op
}
@Override
public int getCount() {
return mEntries.length;
}
@Override
public Object getItem(int position) {
return null; // not applicable
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getDropDownView(position, convertView, parent);
}
@Override
public int getItemViewType(int position) {
return 0;
}
@Override
public int getViewTypeCount() {
return 1;
}
@Override
public boolean isEmpty() {
return false;
}
});
spinner.setSelection(mSelection);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
mSelection = position;
persistString(mEntryValues[position]);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// no op
}
});
}
protected View createDropDownView(int position, ViewGroup parent) {
return mLayoutInflater.inflate(R.layout.spinner_pref_dropdown, parent, false);
}
/**
* Customize dropdown view for given spinner item
* @param position item position
* @param view item view
*/
protected void bindDropDownView(int position, View view) {
TextView textView = (TextView) view.findViewById(android.R.id.text1);
textView.setText(mEntries[position]);
}
}
编辑:我发布了一个示例代码,只显示了这两个问题:
答案 0 :(得分:1)
我遇到了同样的问题,并在这篇文章中发布了答案:
Custom Preference shows differently on Preference screen than native Preferences
它将解决您的问题。
在你的代码中只需将代码从3参数构造函数移动到2参数构造函数(call super(context, attrs);
)并删除3参数构造函数,基类Preference
将为您选择合适的样式