Android的自定义开关部件的开关preferenc [英] android custom Switch widget for SwitchPreferenc
问题描述
我四处搜寻计算器和查找下一个相关主题:
I search around stackoverflow and find next related topics:
- 我怎么可以风格一个Android切换? 李>
- 在Android的自定义开关部件4
- <一个href="http://stackoverflow.com/questions/9006894/set-switchstyle-get-error-resource-not-found-why">Set switchStyle - 得到错误的资源没有发现 - 为什么
- How can i style an Android Switch?
- Custom switch widget in Android 4
- Set switchStyle - get error resource not found - why?
我也觉得错误报告对谷歌组:问题36636:无法覆盖的风格switchStyle 而在最后找到新的probles与开关部件:
I also find bugreport on google group: Issue 36636: Unable to override style switchStyle And at last find new probles with Switch widget:
-
我试图让自己的 preference.Switch preference 并定义与开关部件布局
I tried to make my own Preference.SwitchPreference and define layout with Switch widget
android:id="@+android:id/switchWidget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:thumb="@drawable/switch_thumb"
android:layout_gravity="center"
android:padding="16dip"
android:focusable="false" />
但我得到一个compliation错误:错误:资源是不公开的。 (在ID,值为 @ +安卓ID / switchWidget )。所以,我不能使用这种方式。
but I get an compliation error: Error: Resource is not public. (at 'id' with value '@+android:id/switchWidget'). So I can't use this way.
- 第二个办法,我试图从code扩展开关类添加设置的资源。但我发现这个方法 setThumbResource 是唯一的菱从API 16.但我还是不适用 @ + Android的:ID / switchWidget ,因为它不是公共
- Second way I tried to extend Switch class add set resources from code. But I find that method setThumbResource is availible only from API 16. But I still can't apply @+android:id/switchWidget because it's not public.
所以,我如何获得自定义开关preference的SDK API 15???或者,我怎么可以自定义开关在preferences?
推荐答案
刚刚发现一个可怕的方式来实现这一目标。
Just found an awful way to achieve this.
首先,的src / COM / MyApp的/视图/ preference /是myswitch preference.java
public class MySwitchPreference extends SwitchPreference {
public MySwitchPreference(Context context) {
super(context);
}
public MySwitchPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MySwitchPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
if (view instanceof ViewGroup) {
setLayout((ViewGroup) view);
}
}
@SuppressLint("NewApi")
private void setLayout(ViewGroup viewGroup) {
int count = viewGroup.getChildCount();
for(int n = 0; n < count; ++n) {
View childView = viewGroup.getChildAt(n);
if(childView instanceof Switch) {
final Switch switchView = (Switch) childView;
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
switchView.setThumbResource(com.myapp.R.drawable.switch_inner);
switchView.setTrackResource(com.myapp.R.drawable.switch_track);
}
return;
}
else if (childView instanceof ViewGroup)
setLayout((ViewGroup) childView);
}
}
}
而现在, RES / XML / preferences.xml
<com.myapp.views.preference.MySwitchPreference
android:switchTextOff="Off"
android:switchTextOn="On"
android:title="whatever"
android:key="switch" />
一个有点棘手,只有工作与Android> 16
A little bit tricky, and only working with Android > 16.
这篇关于Android的自定义开关部件的开关preferenc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!