Android 부동 동작 버튼 색상 변경
머티리얼의 Floating Action Button 색상을 변경하려고 했지만 실패했습니다.
<android.support.design.widget.FloatingActionButton
android:id="@+id/profile_edit_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="16dp"
android:clickable="true"
android:src="@drawable/ic_mode_edit_white_24dp" />
추가하려고 했습니다.
android:background="@color/mycolor"
또는 코드 경유:
FloatingActionButton fab = (FloatingActionButton) rootView.findViewById(R.id.profile_edit_fab);
fab.setBackgroundColor(Color.parseColor("#mycolor"));
또는
fab.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#mycolor")));
하지만 위 중 어느 것도 효과가 없었다.제안된 중복 질문의 해결책도 시도해 봤지만, 모두 효과가 없었습니다.버튼은 녹색으로 남아 있고, 사각형으로 되어 있습니다.
P.S. 리플리펙트를 추가하는 방법도 알 수 있으면 좋겠지만, 그것도 이해할 수 없습니다.
설명서에서 설명한 바와 같이 기본적으로 styles.xml Atribut colorAcent에서 설정된 색상을 사용합니다.
이 보기의 배경색은 기본적으로 테마의 colorAcent로 설정됩니다.실행 시 이를 변경할 경우 setBackgroundTintList(ColorStateList)를 통해 변경할 수 있습니다.
색상 변경을 원하시면
- XML 내, 아트리뷰트 앱: backgroundTint
<android.support.design.widget.FloatingActionButton
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_add"
app:backgroundTint="@color/orange"
app:borderWidth="0dp"
app:elevation="6dp"
app:fabSize="normal" >
- .setBackgroundTintList 코드(아래 ywynm으로 답변)
코멘트 @Dantalian에서 언급했듯이 디자인 지원 라이브러리의 아이콘 색상을 v22(포함)까지 변경하는 경우 다음을 사용할 수 있습니다.
android:tint="@color/white"
v23 이후의 설계 지원 라이브러리의 경우 다음을 사용할 수 있습니다.
app:tint="@color/white"
,과 ,androidX라이브러리 xml 레이아웃에서 0dp 테두리를 설정해야 합니다.
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_add"
app:backgroundTint="@color/orange"
app:borderWidth="0dp"
app:elevation="6dp"
app:fabSize="normal" />
Vijet Badiganavar를 사용합니다.ColorStateList보통 복잡해서 어떻게 하는지 알려주지 않았어요. 변경에 있기 View상태이므로
「 」를 변경하는
FAB일 때mFab.setBackgroundTintList(ColorStateList.valueOf(your color in int));「 」를 변경하는
FAB의 색상은 프레스 상태이며, Design Support Library 22.2.1을 통해 기입할 수 있습니다.mFab.setRippleColor(your color in int);Atribute를 으로써 Atribute를 때 를 .
FAB이 터치 의 표면 에 드러나게 됩니다.FABFAB의 색상은 정상 상태입니다. 21 API 21(Lollipop)은 파급효과는 .FAB를 누르면 의 색상이 계속 바뀝니다.
더 복잡한 , .ColorStateList이에 대해 논의하는 SO 질문은 다음과 같습니다.ColorStateList를 프로그래밍 방식으로 작성하려면 어떻게 해야 합니까?
업데이트: @Kaitlyn의 코멘트 감사합니다.backgroundTint를 색상으로 사용하여 FAB의 스트로크를 제거하려면 다음을 설정합니다.app:borderWidth="0dp"를 참조해 주세요.
Vasil Valchev가 코멘트에서 지적했듯이 보기보다 단순하지만 XML에서 알아차리지 못한 미묘한 차이가 있습니다.
<android.support.design.widget.FloatingActionButton
android:id="@+id/profile_edit_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="16dp"
android:clickable="true"
android:src="@drawable/ic_mode_edit_white_24dp"
app:backgroundTint="@android:color/white"/>
주의:
app:backgroundTint="@android:color/white"
가 아니라
android:backgroundTint="@android:color/white"
앱을 사용하여 FAB의 색상을 변경하려고 하면 문제가 있습니다.버튼 프레임의 색상이 다르므로 다음 작업을 수행해야 합니다.
app:backgroundTint="@android:color/transparent"
코드에서는 색상을 설정합니다.
actionButton.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.white)));
그냥 쓰세요.
app:backgroundTint="@color/colorPrimary"
사용 안 함,
android:backgroundTint="@color/colorPrimary"
는 FAB에 이 매겨집니다.colorAccent.
<style name="AppTheme" parent="Base.Theme.AppCompat.Light">
<item name="colorAccent">@color/accent</item>
</style>
mFab.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(mContext,R.color.mColor)));
재료 1.1.0의 부동 동작 버튼에 대한 새 테마 속성 매핑
앱 테마:
- colorSecondary를 설정하여 FAB 배경색을 설정합니다(backgroundTint에 매핑).
- FAB의 아이콘/텍스트 및 리플 색상에 대한 색상을 설정하려면 colorOnSecondary를 설정합니다(tint 및 ripleColor에 매핑).
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- ...whatever else you declare in your app theme.. -->
<!-- Set colorSecondary to change background of FAB (backgroundTint) -->
<item name="colorSecondary">@color/colorSecondary</item>
<!-- Customize colorSecondary to change icon/text of FAB (maps to tint and rippleColor) -->
<item name="colorOnSecondary">@android:color/white</item>
</style>
다른 솔루션이 효과가 있을 수 있습니다.이는 10파운드 고릴라 접근방식으로 다음과 같은 경우에 광범위하게 적용할 수 있다는 장점이 있습니다.
Styles.xml:
<style name="AppTheme.FloatingAccentButtonOverlay" >
<item name="colorAccent">@color/colorFloatingActionBarAccent</item>
</style>
레이아웃 xml:
<android.support.design.widget.FloatingActionButton
android:theme="AppTheme.FloatingAccentButtonOverlay"
...
</android.support.design.widget.FloatingActionButton>
이 문서에서는 디폴트로 @color/accent 를 사용하고 있는 것을 나타내고 있습니다.하지만 코드를 덮어쓰려면
fab.setBackgroundTintList(ColorStateList)
그리고 기억하세요.
이 라이브러리를 사용하려면 최소 API 버전이 15이므로 업데이트해야 합니다! 만약 당신이 그것을 원하지 않는다면, 당신은 커스텀 드로잉 가능을 정의해서 꾸밀 필요가 있습니다!
재료 테마 및 재료 구성요소를FloatingActionButton 사용하면 기본적으로 에 설정된 색상을 사용합니다.styles.xml속성을 지정합니다.
- 이 속성은 xml로 사용할 수 있습니다.
<com.google.android.material.floatingactionbutton.FloatingActionButton
...
app:backgroundTint=".."
app:srcCompat="@drawable/ic_plus_24"/>
하시면 됩니다.
fab.setBackgroundTintList();속성을 사용하여 스타일을 사용자 정의할 수 있습니다.
<!--<item name="floatingActionButtonStyle">@style/Widget.MaterialComponents.FloatingActionButton</item> -->
<style name="MyFloatingActionButton" parent="@style/Widget.MaterialComponents.FloatingActionButton">
<item name="backgroundTint">#00f</item>
<!-- color used by the icon -->
<item name="tint">@color/...</item>
</style>
- 재료 구성요소의 버전 1.1.0부터는 새 속성을 사용하여 일부 구성요소에 대해서만 기본 색상을 재정의할 수 있습니다.
<style name="MyFloatingActionButton" parent="@style/Widget.MaterialComponents.FloatingActionButton">
<item name="materialThemeOverlay">@style/MyFabOverlay</item>
</style>
<style name="MyFabOverlay">
<item name="colorSecondary">@color/custom2</item>
<!-- color used by the icon -->
<item name="colorOnSecondary">@color/...</item>
</style>
아래 줄을 사용하여 이동 수행 단추 배경색 변경
app:backgroundTint="@color/blue"
부동 작업 버튼 아이콘 색상 변경
android:tint="@color/white"
자기완성 덕분이에요.몇 번의 타격과 시련 끝에 나는 운이 좋았다.
xmlns:card_view="http://schemas.android.com/apk/res-auto"
card_view:backgroundTint="@color/whicheverColorYouLike"
-- 또는 -- (둘 다 기본적으로 같은 것)
xmlns:app="http://schemas.android.com/apk/res-auto"
app:backgroundTint="@color/whicheverColorYouLike"
이것은 디자인 라이브러리 23.1.0을 사용한 API 버전 17에서 작동했습니다.
나도 똑같은 문제가 생겨서 머리가 다 꼬여요.https://stackoverflow.com/a/35697105/5228412에 접속해 주셔서 감사합니다.
우리가 할 수 있는 건..
favourite_fab.setImageDrawable(ContextCompat.getDrawable(getBaseContext(), R.drawable.favourite_selected));
저는 잘 되고 여기 올 수 있는 다른 분들도 있으면 좋겠습니다.
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
app:elevation="6dp"
app:backgroundTint="@color/colorAccent"
app:pressedTranslationZ="12dp"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/add"/>
res/values/color.xml에 색상을 추가하고 fab에 속성을 포함합니다.
app:backgroundTint="@color/addedColor"
데이터 바인딩을 사용하면 다음과 같은 작업을 수행할 수 있습니다.
android:backgroundTint="@{item.selected ? @color/selected : @color/unselected}"
나는 아주 간단한 예를 들었다.
그리기 가능한 부동 동작 버튼이 없는 경우 다음을 사용하여 프로그래밍 방식으로 색조를 변경할 수 있습니다.
fab.getBackground().mutate().setTint(ContextCompat.getColor(yourContext, R.color.anyColor));
재료 디자인은 플로팅 액션 버튼 색상을 이렇게 변경했을 뿐입니다.플로팅 액션 버튼 xml에 아래 두 줄을 추가합니다.그리고 다 했어요.
android:backgroundTint="@color/colorPrimaryDark"
app:borderWidth="0dp"
Android:background="@color/colorAcent" 폴더 해상도로 이동한 다음 폴더 값을 클릭한 다음 colors.xml 색상Acent를 변경하고 Android:background로 호출합니다.
버튼의 색상을 설정하기 전에 이 색상에 대해 원하는 값을 설정하는 것이 중요합니다.values > color로 이동합니다.기본 색상도 찾을 수 있지만, 색상 및 이름을 변경하여 색상을 결합 및 붙여넣을 수도 있습니다.그러면...(activity_main에서) 플로팅 버튼의 색상을 변경할 때 작성한 버튼을 선택할 수 있습니다.
예 - 기본색 + 3가지 색상의 값 코드 > 색상 추가:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="corBotaoFoto">#f52411</color>
<color name="corPar">#8e8f93</color>
<color name="corImpar">#494848</color>
</resources>
이제 내가 만들고 "corPar"라는 이름을 붙인 색상을 가진 Floating Action 버튼:
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_input_add"
android:tint="#ffffff"
app:backgroundTint="@color/corPar"/>
그것은 나에게 효과가 있었다.행운을 빕니다.
데이터 바인딩과 함께 작동했던 솔루션
val color = ContextCompat.getColor(context, R.color.colorPrimary)
binding.fab.backgroundTintList = ColorStateList.valueOf(getColor)
코틀린:
val gray = getColor(requireContext(), R.color.green)
binding.fabSubmit.backgroundTintList = ColorStateList.valueOf(gray)
color.xml 파일에 색상을 추가한 다음 이 코드 행을 추가합니다. floatingActionButton.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.fab2_color)));
프로그래밍 방식으로 색상을 변경하려는 경우 이 코드를 사용할 수 있습니다.
floating.setBackgroundTintList(getResources().getColorStateList(R.color.vermelho));
사용하다
앱: backgroundTint="@color/orange" 인
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/id_share_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/share"
app:backgroundTint="@color/orange"
app:fabSize="mini"
app:layout_anchorGravity="end|bottom|center" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
언급URL : https://stackoverflow.com/questions/30969455/android-changing-floating-action-button-color
'source' 카테고리의 다른 글
| Vue 3: 상위 항목이 데이터 가져오기가 완료될 때까지 기다렸다가 하위 데이터를 가져오고 로더를 표시합니다. (0) | 2022.07.17 |
|---|---|
| 음, 당신은 PRIU64가 누군가요? (0) | 2022.07.17 |
| 왜 구조 자체보다는 구조의 첫 번째 요소의 주소를 사용하는가? (0) | 2022.07.17 |
| Vuex 및 타이프스크립트를 사용하는 올바른 방법은 무엇입니까? (0) | 2022.07.17 |
| vue.js 템플릿에 외부 스크립트 포함 (0) | 2022.07.17 |

