更新时间:2023-11-17 15:34:40
地狱的人,我喜欢你的想法: - )
Hell man, I like your idea :-)
这仅仅是同@ MH的答案,但更简洁。
This is just same as @MH's answer, but more concise.
我用切换按钮
测试,而不是开关
。
package android.dumdum;
import android.content.Context;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ToggleButton;
public class TogglePreference extends Preference {
public TogglePreference(Context context) {
super(context);
}
public TogglePreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TogglePreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public View getView(View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = new LinearLayout(getContext());
((LinearLayout) convertView)
.setOrientation(LinearLayout.HORIZONTAL);
TextView txtInfo = new TextView(getContext());
txtInfo.setText("Test");
((LinearLayout) convertView).addView(txtInfo,
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1));
ToggleButton btn = new ToggleButton(getContext());
((LinearLayout) convertView).addView(btn);
}
return convertView;
}
}
而 preferences.xml
:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="Test custom preferences" >
<android.dumdum.EncryptorEditTextPreference />
<android.dumdum.TogglePreference />
</PreferenceCategory>
</PreferenceScreen>
EncryptorEditText preference
不涉及您的问题,但它使用同样的技术(延长的EditText preference
)。
EncryptorEditTextPreference
is not related to your question, but it uses same technique (extending EditTextPreference
).
这说明: