나는 http://www.hidroh.com/2015/11/30/building-custom-preferences-v7/사용자 환경 설정 스타일의 패딩 문제
에서이 같은 지금까지 확장 한 기본 설정을 같은 스피너 환경 설정을 통해 만들려고 오전 :
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]);
}
}
그러나이 결과는 다음과 : 당신은 "서버"제목을 볼 수 있듯이
는의 나머지 부분과는 다른 모습 기본 설정, 비록 스타일을 건드리지 않습니다. 패딩은 매우 다르다. 왜 이런 일이 일어나고이 스타일을 어떻게 수정할 수 있습니까?
또한 동일한 이미지에서 아이콘을 볼 수 있습니다.이 아이콘을 맨 위에 맞출 수 있습니까?
기본 설정 XML :
<android.support.v7.preference.PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.preference.ListPreference
android:key="prefs-preferred-quality"
android:icon="@drawable/ic_soccer"
android:summary="Bitrate determines the quality of the stream. Auto is recommended."
android:title="Streaming bitrate"/>
<android.support.v7.preference.SwitchPreferenceCompat
android:defaultValue="false"
android:key="prefs-record-locally"
android:summary="Should videos also be saved to the device?"
android:title="Record Locally"/>
<tv.mycujoo.mycujoobroadcast.settings.preferences.SpinnerPreference
android:key="prefs-preferred-server"
android:icon="@drawable/ic_record_dark"
app:entries="@array/stream_bitrate"
app:entryValues="@array/stream_bitrate_values"
android:summary="Streaming region to stream to"
android:title="Server"/>
</android.support.v7.preference.PreferenceScreen>
편집 :
https://github.com/kelmer44/prefs-test
github에 간단한 프로젝트를 게시 할 수 있습니까? – azizbekian
나는 방금 @azizbekian을 만들었습니다 – MichelReap