繁体   English   中英

从偏好中移除涟漪效应

[英]Remove Ripple Effect from Preference

我想要一个可点击但隐藏的偏好,没有涟漪效应。

我尝试在 XML 中添加按钮、创建自定义首选项和禁用某些东西,但涟漪效应仍然存在。

如何创建可单击但隐藏且没有涟漪效应的首选项?

XML

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <SwitchPreference
            android:defaultValue="false"
            android:persistent="false"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:clickable="true"
            android:visibility="gone"
            android:visible="false"
            android:background="@drawable/bg_transparent"
            android:textColor="@color/transparent"
            android:switchTextAppearance="@color/transparent"
            android:button="@color/transparent"
            android:cursorVisible="false"
            app:rippleColor="@android:color/transparent"
            tools:visibility="gone"
            android:animateLayoutChanges="false"
            android:animationCache="false"
            android:animation="@color/transparent"
            android:background="@drawable/dt_transparent"
            android:listSelector="@android:color/transparent"
            android:title="" />

    <mypreferences.HiddenPreference
            android:clickable="true"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:foreground="@color/transparent"
            android:key="UnlockSelection"
            android:layoutAnimation="@color/transparent"
            android:longClickable="false"
            android:overScrollMode="never"
            android:soundEffectsEnabled="false"
            android:visibility="gone"
            android:visible="false"
            app:rippleColor="@android:color/transparent"
            app:tabRippleColor="@android:color/transparent"
            app:tabIndicatorColor="@android:color/transparent"
            tools:visibility="gone" />
</PreferenceScreen>

HiddenPreference.java

public class HiddenPreference extends Preference
{
    @SuppressWarnings("unused")
    public HiddenPreference(Context context) {
        super(context);
    }
    
    @SuppressWarnings("unused")
    public HiddenPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    @Override
    protected View onCreateView(ViewGroup parent)
    {
        Resources res = getContext().getResources();
        View v = super.onCreateView(parent);
        v.setAnimation(null);
        v.setEnabled(false);
        v.setBackground(res.getDrawable(R.drawable.dt_transparent)); // R.drawable.dt_transparent is a transparent .png file
        // v.setBackground(null); // -- This didn't work either
        v.setBackgroundColor(Color.TRANSPARENT);
        v.setAlpha(0);
        v.setSoundEffectsEnabled(false);
        v.setSystemUiVisibility(View.GONE);
        v.setWillNotDraw(true);
        v.clearAnimation();
        v.setVisibility(View.GONE);
        v.setClickable(true);
        return v;
    }
}

我最终创建了一个带有透明ImageLinearLayout ,然后我使用LinearLayout作为我想要隐藏的首选项的布局。

偏好屏幕 XML

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <com.compay.preferences.HiddenPreference
        android:clickable="true"
        android:key="myHiddenPreference" />

    <!-- Add other preferences here -->
</PreferenceScreen>

hidden_preference.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/widget_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/hiddenPreference"
        android:clickable="true"
        android:focusable="true"
        android:soundEffectsEnabled="false"
        android:background="@drawable/dt_transparent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="hiddenPreference"
         />

</LinearLayout>

HiddenPreference.java

public class HiddenPreference extends Preference
{
    @SuppressWarnings("unused")
    public HiddenPreference(Context context) {
        super(context);
    }

    @SuppressWarnings("unused")
    public HiddenPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected View onCreateView(ViewGroup parent)
    {
        View v = super.onCreateView(parent);
        LayoutInflater li = (LayoutInflater)v.getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );

        return li.inflate(R.layout.hidden_preference, parent, false);
    }

    @Override
    protected void onBindView(View view)
    {
        view.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, 20));
        view.setSoundEffectsEnabled(false);

        view.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                // Handle click event
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM