Programing

? attr / selectableItemBackground를 배경으로 사용할 때 잔물결 색상을 어떻게 수정할 수 있습니까?

lottogame 2020. 9. 18. 19:11
반응형

? attr / selectableItemBackground를 배경으로 사용할 때 잔물결 색상을 어떻게 수정할 수 있습니까?


나는 몇 가지 질문을 보았고 내가 원하는 것을 달성하기 위해 가능한 몇 가지 방법을 제공했습니다. 예를 들면 :

  1. colorControlHighlightstyles.xml에서 속성을 사용하십시오 .

    내 styles-v21.xml은 다음과 같습니다.

    <style name="SelectableItemBackground">
        <item name="android:colorControlHighlight">#5677FC</item>
        <item name="android:background">?attr/selectableItemBackground</item>
    </style>
    

    그리고 내 위젯 :

    <TextView
        android:id="@+id/tv_take_photo_as_bt"
        android:layout_width="280dp"
        android:layout_height="48dp"
        android:text="@string/act_take_photo"
        style="@style/SelectableItemBackground"/>
    

    그리고 그것은 작동하지 않습니다. 또한 추가하려고하지 parent="Theme.AppCompat"SelectableItemBackground"스타일 또는 변경하려면 colorControlHighlight(no android: prefix)"에, 또는 변경 ?android:attr/selectableItemBackground, 어느 쪽도 유용하다.

  2. backgroundTint레이아웃에서 속성을 사용 합니다.

    그래서 나는 android:backgroundTint="#5677FC"TextView. 여전히 쓸모가 없습니다. 그럼 변경 시도 android:backgroundTintModesrc_in하고 src_atop, 그들은 변화를하지 않습니다.

그래서 ?attr/selectableItemBackground배경으로 사용할 때 잔물결 색상을 어떻게 바꿀 수 있습니까 ? 나는 Lollipop 이상에만 집중합니다. 미리 감사드립니다!


마지막으로 해결책을 찾았습니다. android:colorControlHighlighttheme SelectableItemBackground에서 직접 사용하는 대신 다른 스타일을 작성해야합니다.

<style name="SelectableItemTheme">
    <item name="colorControlHighlight">@color/ripple_color</item>
</style>

그때:

<style name="SelectableItemBackground">
    <item name="android:theme">@style/SelectableItemTheme</item>
    <item name="android:background">?attr/selectableItemBackground</item>
</style>

마지막으로 추가 style="@style/SelectableItemBackground"Viewlayout.xml에.

2016년 8월 26일 갱신 N의 출시 후, 나는 가끔 우리는 어떤 종류의 리플 색상을 설정하려면이 방법을 사용할 수 없습니다 발견 View(예를 들어,을 CardView). 이제는 RippleDrawablexml로 선언 할 수 있는를 사용 하는 개발자를 적극 권장 합니다. 다음은 예입니다.

사용자가 CardView위의 API21을 터치 / 클릭 할 때 파급 효과를 보여주고 싶습니다. 물론 Lollipop 이전에 다른 종류의 피드백이 있어야합니다. 따라서 다음과 같이 작성해야합니다.

<android.support.v7.widget.CardView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:foreground="@drawable/selectable_item_background"/>

selectable_item_background에서 drawable폴더 :

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false" android:drawable="@android:color/transparent" />
    <item android:drawable="@color/color_clicked" />
</selector>

selectable_item_background에서 drawable-v21폴더 :

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ripple_black" />
</selector>

마지막으로 ripple_blackin drawable(또는 drawable-v21) 폴더 :

<ripple
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:color="@color/color_clicked"
    tools:ignore="NewApi" /> <!--you can remove this line if it's in v21 folder-->

That's it. For other views, maybe you should use android:background="@drawable/selectable_item_background". Don't forget to set an OnClickListener, OnTouchListener or something like those for them, otherwise ripple won't show.


Ripple effect on pre- and Lollipop+ devices

harrane and Liuting are right. The accepted answer is not the best way. Let me show in code how to change ripple color for pre-Lollipop versions and higher

  1. Your AppTheme should inherit from any AppCompat theme and contain colorControlHighlight attribute (without 'android:' prefix)

    <!-- Application theme. -->
    <style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
    <item name="colorControlHighlight">#40ffffff</item>
    </style>
    
  2. Your view should contain clickable="true" (or should have a click listener set programmatically) and background should be "?attr/selectableItemBackgroundBorderless" or "?attr/selectableItemBackground" :

    <LinearLayout
    ...
    android:clickable="true"
    android:background="?attr/selectableItemBackgroundBorderless"/>
    

Note: that if your parent view has white background you won't see ripple effect since it's white. Change colorControlHighlight value for a different color

Also, if you want different ripple colors on different activities you can set personal theme for each activity in Manifest file, for example:

       <activity
        android:name="com.myapp.GalleryActivity"
        android:theme="@style/RedRippleTheme"
        />

Different ripple colors for different fragments in the same activity?

You can change attributes of Activity Theme for each fragment in runtime. Just overwrite them before fragment was inflated with your custom style and apply to a current Theme:

in values/styles.xml

    <style name="colorControlHighlight_blue">
       <item name="colorControlHighlight">@color/main_blue_alpha26</item>
    </style>

Then, in your fragment before inflation in onCreateView():

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       getContext().getTheme().applyStyle(R.style.colorControlHighlight_blue, true); //blue ripple color
       View view = inflater.inflate(R.layout.my_fragment_layout, container, false);
       return view;
    }

This style will work only for this fragment


Different ripple color for different Views? (Lollipop +)

You can change the ripple color for each view seperately using colorControlHighlight attribute, it doesn't work if you apply them to a view directly:

    <TextView
     ...
     colorControlHighlight="#40ffffff"/> <!-- DOESN'T WORK -->

you should apply it as a theme:

<TextView
  ...
  android:theme="@style/colorControlHighlight_blue"/>

P.S. Also, sometimes this approach helps if you have unknown issues with ripple and you can't figure it out. In my case, I used 3rd party sliding lib which messed up ripple effects for the entire layout and adding explicitly this theme to all clickable views worked out for me.


The accepted answer is wrong.

The correct way to use is what Liuting mentioned in the comment. Use colorControlHighlight instead of android:colorControlHighlight for changing the default colorControlHighlight from AppCompat

* Please refer to http://android-developers.blogspot.co.uk/2014/10/appcompat-v21-material-design-for-pre.html in the Theming section *


It's showing ripple effect with color on API +21, and simple gray background on press for API -21. Add this style:

<style name="AppTheme.MyRipple">
   <item name="colorControlHighlight">@color/your_color</item>
   <item name="android:background">?selectableItemBackgroundBorderless</item>
</style>

And set it to the view:

<Button
   ...
   android:theme="@style/AppTheme.MyRipple" />

Use the foreground attribute as selectableItemBackground and background attribute as the color you want.

android:foreground="?attr/selectableItemBackground"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:background="@color/white"

This code works for me to create a ripple:

public static void setRippleDrawable(View view, int normalColor, int touchColor) {
    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            RippleDrawable rippleDrawable = new RippleDrawable(ColorStateList.valueOf(touchColor), view.getBackground(), null);
            view.setBackground(rippleDrawable);
        } else {
            StateListDrawable stateListDrawable = new StateListDrawable();
            stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, new ColorDrawable(touchColor));
            stateListDrawable.addState(new int[]{android.R.attr.state_focused}, new ColorDrawable(touchColor));
            stateListDrawable.addState(new int[]{}, new ColorDrawable(normalColor));
            view.setBackground(stateListDrawable);
            }
    } catch (Exception e) {
        Log.e(LOG_TAG, "" + e);
    }
}

I did not found any way to modify the selectableItemBackground attribute. That's why I did it like above.


In dark black theme(Or any other) app try to use like below first create ripple_effect.xml in drawable folder and add code like

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#f5f5f5">
    <item android:id="@android:id/mask">
        <shape android:shape="rectangle">
            <solid android:color="#f5f5f5" />

        </shape>
    </item>

</ripple>

then set background to your Any view like Linear layout, Button, TextView etc.

 <TextView
                    android:id="@+id/tvApply"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/ripple_effect"
                    android:clickable="true"
                    android:text="APPLY"
                    android:textColor="#FFFFFF"
                    android:gravity="center"
                    android:textSize="@dimen/_8sdp"
                    android:padding="@dimen/_8sdp"
                    android:focusable="true" />

Use the below steps:

1. Make changes to button view in your layout.xml
2. Add new styles in styles.xml

your_layout.xml

<Button
                        android:id="@+id/setup_submit_button"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="16dp"
                        android:text="@string/action_sign_in"
                        android:textStyle="bold"
                        android:background="@color/colorPrimary"
                        android:textColor="@color/white"
                        style="@style/SelectableItemBackground"
                        android:foreground="?android:attr/selectableItemBackground"/>

-The style attribute calls the style that we created.

-Foreground attribute calls the andorid's default selectable attribute.

styles.xml

 <style name="SelectableItemTheme">
        <item name="colorControlHighlight">@color/white</item>
    </style>


    <style name="SelectableItemBackground">
        <item name="android:theme">@style/SelectableItemTheme</item>
        <item name="android:background">?attr/selectableItemBackground</item>
    </style>

참고URL : https://stackoverflow.com/questions/33061050/how-can-i-modify-ripple-color-when-using-attr-selectableitembackground-as-backg

반응형