在ICS兼容android-support-v4库中引入SwitchPreference
?我试图更新一些旧的项目,如果可能的话,想使用SwitchPreferences。SwitchPreference(pre ICS)的兼容性?
我知道我可以制作一个单独的资源文件来区分API版本,但我想尽可能避免这种情况。
在ICS兼容android-support-v4库中引入SwitchPreference
?我试图更新一些旧的项目,如果可能的话,想使用SwitchPreferences。SwitchPreference(pre ICS)的兼容性?
我知道我可以制作一个单独的资源文件来区分API版本,但我想尽可能避免这种情况。
ICS中引入的SwitchPreference是否与android-support-v4库兼容?
不,对不起。
但是,如果还没有人支持它,它不应该太棘手。
实际上,它可能有点痛苦,因为它也需要一个Switch的backport,并且backporting widgets有时很麻烦,因为它们经常使用backports无法访问的package-private方法。
我知道我可以制作一个单独的资源文件来区分API版本,但是我想尽可能避免这种情况。
好吧,那肯定会比其它方法更简单:
上述反向移植
创造某种别名Preference
机制,允许您使用较新设备上SwitchPreference
和CheckBoxPreference
只有一个资源文件的旧设备上
这就是我所怀疑的。谢谢! –
@CommonsWare为什么不在同一个包中创建自定义开关呢? –
@ChristopherPerry:我不知道你在说什么,对不起。 – CommonsWare
android-switch-backport有一个适用于Android 2.1+的SwitchPreference。
我不喜欢这些东西,他们不能将它们捆绑在一起并作为jar包含在应用程序中。相反,您必须将许多(81)res文件放入您的应用程序,这会淹没您自己的资源。它的遗憾是Android支持库不包括它。 – Doomsknight
这个指令和支持要好得多 - https://github.com/ankri/SwitchCompatLibrary – LoungeKatt
@TwistedUmbrella SwitchCompatLibrary不提供SwitchPreference,所以对这个问题没有帮助。 – Intrications
我试过每一个解决方案,我发现,但其中不被满足我的需要,所以我创建了自己的小部件至极从nineOld兼容性LIB使用ObjectAnimator和工作得罚款任何Android API。
import android.widget.RelativeLayout;
import com.myapp.utilities.AppUtils;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.AnimatorListenerAdapter;
import com.nineoldandroids.animation.ObjectAnimator;
public class SwitchButton extends RelativeLayout {
public static final int TEXT_SIZE = 11;
public float HANDLE_SHIFT = -40f;
public float TEXT_RIGHT_SHIFT = 40f;
public static int BUTTON_ID = 0x00009999;
public static int TEXT_ID = 0x00008888;
private Button handleButton;
private RoboTextView textView;
private boolean switchEnabled;
private String yesStr;
private String noStr;
private int TEXT_LEFT_PADDING = 13;
private ObjectAnimator animateHandleLeftShift;
private ObjectAnimator animateHandleRightShift;
private int HANDLE_BUTTON_HEIGHT = 22;
private int HANDLE_BUTTON_WIDTH = 42;
private ObjectAnimator animateTextLeftShift;
private ObjectAnimator animateTextRightShift;
public SwitchButton(Context context) {
super(context);
onCreate(context);
}
private void onCreate(Context context) {
float density = context.getResources().getDisplayMetrics().density;
TEXT_LEFT_PADDING *= density;
HANDLE_BUTTON_HEIGHT *= density;
HANDLE_BUTTON_WIDTH *= density;
HANDLE_SHIFT *= density;
TEXT_RIGHT_SHIFT *= density;
yesStr = getContext().getString(R.string.yes).toUpperCase();
noStr = getContext().getString(R.string.no).toUpperCase();
{// Button
handleButton = new Button(getContext());
RelativeLayout.LayoutParams buttonParams = new LayoutParams(HANDLE_BUTTON_WIDTH, HANDLE_BUTTON_HEIGHT);
buttonParams.addRule(RelativeLayout.CENTER_VERTICAL);
buttonParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
handleButton.setBackgroundResource(R.drawable.button_switch_handle_selector);
handleButton.setId(BUTTON_ID);
addView(handleButton, buttonParams);
}
{// Text
textView = new RoboTextView(getContext());
LayoutParams textParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
textParams.addRule(RelativeLayout.CENTER_VERTICAL);
textView.setText(yesStr);
textView.setTextColor(getContext().getResources().getColor(R.color.new_normal_gray));
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, TEXT_SIZE);
textView.setPadding(TEXT_LEFT_PADDING, 0, 0, 0);
textView.setFont(RoboTextView.ROBOTO_BOLD_FONT);
textView.setId(TEXT_ID);
float shadowRadius = 0.5f ;
float shadowDx = 0;
float shadowDy = 1;
textView.setShadowLayer(shadowRadius, shadowDx, shadowDy, Color.BLACK);
addView(textView, textParams);
}
initFlipAnimation();
}
@Override
public void setOnClickListener(OnClickListener l) {
handleButton.setOnClickListener(l);
textView.setOnClickListener(l);
}
public void toggle(View view){
if (AppUtils.HONEYCOMB_PLUS_API && view.getId() == TEXT_ID) { // ignore text clicks
return;
}
switchEnabled = !switchEnabled;
if (switchEnabled) {
// animate handle to the left
animateHandleLeftShift.start();
animateTextLeftShift.start();
textView.setText(noStr);
} else {
animateHandleRightShift.start();
animateTextRightShift.start();
textView.setText(yesStr);
}
}
private android.view.animation.Interpolator accelerator = new LinearInterpolator();
private static final int DURATION = 70;
private void initFlipAnimation() {
animateHandleLeftShift = ObjectAnimator.ofFloat(handleButton, "translationX", 0f, HANDLE_SHIFT);
animateHandleLeftShift.setDuration(DURATION);
animateHandleLeftShift.setInterpolator(accelerator);
animateHandleRightShift = ObjectAnimator.ofFloat(handleButton, "translationX", HANDLE_SHIFT, 0f);
animateHandleRightShift.setDuration(DURATION);
animateHandleRightShift.setInterpolator(accelerator);
animateHandleLeftShift.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator anim) {
// TODO
}
});
animateTextLeftShift = ObjectAnimator.ofFloat(textView, "translationX", 0f, TEXT_RIGHT_SHIFT);
animateTextLeftShift.setDuration(DURATION);
animateTextLeftShift.setInterpolator(accelerator);
animateTextRightShift = ObjectAnimator.ofFloat(textView, "translationX", TEXT_RIGHT_SHIFT, 0f);
animateTextRightShift.setDuration(DURATION);
animateTextRightShift.setInterpolator(accelerator);
}
}
在XML
<com.chess.SwitchButton
android:id="@+id/ratedGameSwitch"
android:layout_width="@dimen/button_switch_width"
android:layout_height="@dimen/button_switch_height"
android:background="@drawable/button_switch_back"
/>
在活动/片段,你只需要findViewById,并设置clickListener它,并在的onClick回调处理:
switchButton = (SwitchButton) optionsView.findViewById(R.id.ratedGameSwitch);
switchButton.setOnClickListener(this);
@Override
public void onClick(View view) {
if (view.getId() == SwitchButton.BUTTON_ID || view.getId() == SwitchButton.TEXT_ID){
switchButton.toggle(view);
}
}
如果您想以编程方式创建设置活动,请尝试此解决方案。
public class SettingsActivity extends PreferenceActivity {
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PreferenceScreen rootScreen = getPreferenceManager()
.createPreferenceScreen(this);
setPreferenceScreen(rootScreen);
Preference NotifCheck=null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
NotifCheck = new SwitchPreference(this);
} else {
NotifCheck = new CheckBoxPreference(this);
}
NotifCheck.setKey("yourKey");
NotifCheck.setTitle(R.string.ShowNotification);
NotifCheck.setEnabled(true);
rootScreen.addPreference(NotifCheck);
}
}
简短回答是否定的,目前'SwitchPreference'在支持库中不可用。但是,如果还没有人支持它,它不应该太棘手。 –