Lollipop의 backgroundTint는 버튼에 영향을주지 않습니다.
내 활동에 버튼이 있고 내 테마의 강조 색상을 사용하고 싶습니다. 사전 롤리팝을해야하는 것처럼 내 자신의 드로어 블을 만드는 대신 자연스럽게 새 backgroundTint속성 을 사용하고 싶습니다 .
<Button
android:id="@+id/btnAddCode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@color/accent"
android:text="@string/addressInfo_edit_addCode" />
불행히도 효과가 없으며 버튼이 회색으로 유지됩니다.
에 대해 다른 값을 시도했지만 backgroundTintMode아무것도 변경하지 않았습니다.
나는 또한 내 활동에서 프로그래밍 방식으로 시도했지만 아무것도 변경하지 않았습니다.
addCodeView.findViewById(R.id.btnAddCode).setBackgroundTintList(
getResources().getColorStateList(R.color.accent));
내 색조가 무시되는 이유는 무엇입니까?
편집 : 명확히하기 위해 실제로 Lollipop 장치에서 테스트하고 있습니다. 다른 위젯 (예 : EditText)은 정확하고 자동으로 착색됩니다.
API 19에서 API 27까지 테스트
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.AppCompatButton
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/retry"
android:textColor="@android:color/white"
app:backgroundTint="@android:color/holo_red_dark" />
다음과 같이 출력을 생성합니다.
나쁜 소식
BoD가 말했듯이 Lollipop 5.0 (API 레벨 21)에서 Button의 배경에 색을 입히는 것은 의미가 없습니다.
좋은 뉴스
Lollipop 5.1 (API 레벨 22)은 btn_mtrl_default_shape.xml (다른 파일 중에서)을 변경하여이 문제를 해결 한 것 같습니다. https://android.googlesource.com/platform/frameworks/base/+/6dfa60f33ca6018959ebff1efde82db7d2aed1e3%5E!/#F0
좋은 소식
새로운 지원 라이브러리 (버전 22.1+) 는 AppCompatButton을 포함한 많은 구성 요소에 이전 버전과 호환되는 색조 지원 을 추가합니다 !
불행하게도, android:backgroundTint속성은 아직도 일을 (어쩌면 내가 뭔가 잘못하고 있어요) 않습니다 - 당신은 설정해야하므로 ColorStateList사용하여 코드에서 setSupportBackgroundTintList(). android:backgroundTint앞으로 지원되는 모습을 보면 정말 좋을 것 같습니다. 업데이트 : Marcio Granzotto app:backgroundTint가 AppCompatButton에서 작동하는 댓글을 달았습니다 ! 그것의 것을 참고 app:하지, android:그것을 응용 프로그램 / 라이브러리에 있기 때문에.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<AppCompatButton
android:id="@+id/mybutton"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Testing, testing"
app:backgroundTint="#ff00ff"/>
</LinearLayout>
에서 상속하도록 허용하면 활동이 AppCompatButton정상 대신 자동으로 팽창 Button됩니다 AppCompatActivity.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AppCompatButton v = (AppCompatButton) findViewById(R.id.mybutton);
ColorStateList csl = new ColorStateList(new int[][]{new int[0]}, new int[]{0xffffcc00});
v.setSupportBackgroundTintList(csl);
}
}
물론 ColorStateList색상 리소스에서 가져와야하지만 나는 게으 르기 때문에 ...
아, 그리고 테마 중 하나를 기반으로 앱 테마를 만드는 것을 잊지 마십시오. 그렇지 않으면 Theme.AppCompat호환되는 뷰가 매우 슬프게 될 것입니다 ...;)
이것은 2.3.7 (Gingerbread MR1)과 5.0 (Lollipop 'Classic') 모두에서 작동했습니다.
잔물결 드로어 블에 색조를 지정하는 것은 의미가없는 것 같습니다 (그리고 버튼의 기본 배경은 잔물결 드로어 블입니다).
사실, 플랫폼의 기본 버튼 드로어 블을 살펴본 후 "올바른"방법을 찾았습니다. 테마에서 이것을 정의해야합니다.
<item name="android:colorButtonNormal">@color/accent</item>
(물론 이것은 레벨 21 이상에만 해당됩니다.)
경고 : 이것은 테마에 정의되어 있으므로 모든 버튼 (최소한 해당 테마를 사용하는 활동의 모든 버튼)에 지정된 색상을 사용합니다.
보너스로 다음을 정의하여 잔물결 색상을 변경할 수도 있습니다.
<item name="android:colorControlHighlight">@color/accent_ripple</item>
Android 5.0.x에서 착색과 관련된 문제를 해결하려면 다음과 같이 사용합니다.
public static void setButtonTint(Button button, ColorStateList tint) {
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP && button instanceof AppCompatButton) {
((AppCompatButton) button).setSupportBackgroundTintList(tint);
} else {
ViewCompat.setBackgroundTintList(button, tint);
}
}
API 21에 대해서만 지원 방법을 사용하고 다른 모든 경우에는 ViewCompat 하나를 사용합니다.
Just use app:backgroundTint instead of android:backgroundTint, the tint will take effect below Lollipop. The reason is AppCompatActivity use AppCompatViewInflater to auto change Button or TextView to AppCompatButton or AppCompatTextView, then app:backgroundTint take effect.
In my project I used it, it worked.
I usually do it dynamically by using PorterDuff:
mbutton = (Button) findViewById(R.id.mybutton);
mbutton.getBackground().setColorFilter(anycolor, PorterDuff.Mode.MULTIPLY);
You can check different blending modes here and nice examples here.
I think you need to have android:background set to make android:backgroundTint work.
To be more accurate, my guess is that you can't backgroundTint the default button background from Material themes, which is defined as a RippleDrawable.
Similar issue was reported on google https://code.google.com/p/android/issues/detail?id=201873
But after release of Android Support Library, revision 23.2.1 (March 2016) This bug is solved.
Issue : FloatingActionButton.setBackgroundTintList(@Nullable ColorStateList tint) no longer changes background color
update Support Library to Android Support Library to 23.2.1
Use design support library(23.2.1) and appcompatwidgets as below
Material Design for Pre-Lollipop Devices :
AppCompat (aka ActionBarCompat) started out as a backport of the Android 4.0 ActionBar API for devices running on Gingerbread, providing a common API layer on top of the backported implementation and the framework implementation. AppCompat v21 delivers an API and feature-set that is up-to-date with Android 5.0
Android Support Library 22.1 :
The ability to tint widgets automatically when using AppCompat is incredibly helpful in keeping strong branding and consistency throughout your app. This is done automatically when inflating layouts - replacing Button with AppCompatButton, TextView with AppCompatTextView, etc. to ensure that each could support tinting. In this release, those tint aware widgets are now publicly available, allowing you to keep tinting support even if you need to subclass one of the supported widgets.
If we look into the source code of Support Library, we see that it tints normally it's known buttons, but if we change the shape of our button (I have round button) tint doesn't work ok in api<=21. We can also see that TintManager became public class (appcompat-v7:23.1.1), so we can take ColorStateList from default button shape (which is tinted ok in 5.0) for current theme (so we don't have to create the array of colors):
Context c = ...; // activity
AppCompatButton ab = ...; // your button
// works ok in 22+:
if (Build.VERSION.SDK_INT <= 21) {
// default appcompat button, that is tinted ok with current theme colors "abc_btn_default_mtrl_shape":
// ColorStateList tint = TintManager.get(c).getTintList(R.drawable.abc_btn_default_mtrl_shape);
// Appcompat 23.2 change:
ColorStateList tint = AppCompatDrawableManager.get().getTintList(c, R.drawable.abc_btn_default_mtrl_shape);
ab.setSupportBackgroundTintList(tint);
}
Just use app:backgroundTint instead of android:backgroundTint
Because attribute backgroundTint is only used in API level 21 and higher
Be aware recyclerview most updated lib can cause this bug as well.
This command
sendBtnView.setBackgroundTintList(colorState)
worked perfectly in the past but stop working for me. after research it turns out the cause is the lib that was addeded to gradle dependencies:
compile 'com.android.support:recyclerview-v7:+'
그래서 여기 Amit Vaghela 게시물에서 권장 한대로 23.02.1로 변경하려고했습니다. 나는 변했다
compile 'com.android.support:recyclerview-v7:23.02.1'
그러나 gradle 오류는 recyclerview lib 에이 버전 (23.02.1)이 없다고 말했습니다 (gradle은 Jcenter raw.github 또는 repo에서 찾을 수 없음).
그런 다음 내가 gradle 종속성에있는 다른 모든 libs에서 버전 22.02.0 '으로 과거에 잘 작동하는 데 사용 된 setBackgroundTintList 명령을 알고 있었기 때문입니다. 그래서 다음과 같이 변경합니다.
compile 'com.android.support:recyclerview-v7:22.02.0'
이제 다시 작동합니다.
이것이 권장되는지 확실하지 않지만 시도해 볼 수 있습니다.
Drawable newDrawable = mBtnAction.getBackground(); // obtain Bg drawable from the button as a new drawable
DrawableCompat.setTint(newDrawable, mActivity.getHomeTobBarBGColor()); //set it's tint
mBtnAction.setBackground(newDrawable); //apply back to button
일반적으로 작동합니다. 시도 ViewCompat했지만 제대로 작동하지 않는 것 같습니다.
버전 backgroundTint <android.support.design.button.MaterialButton과 함께 사용할 수 있습니다."com.android.support:design:28.0.0-rc01"
참고 URL : https://stackoverflow.com/questions/27735890/lollipops-backgroundtint-has-no-effect-on-a-button
'developer tip' 카테고리의 다른 글
| ViewModel에서 창 닫기 (0) | 2020.10.11 |
|---|---|
| PHP 메일러 다중 주소 (0) | 2020.10.11 |
| 이 응용 프로그램에는 / error에 대한 명시 적 매핑이 없습니다. (0) | 2020.10.11 |
| SQL Server에서 동일한 예외를 다시 발생시키는 방법 (0) | 2020.10.11 |
| 런타임에 Android에서 텍스트의 일부를 굵게 만드는 방법은 무엇입니까? (0) | 2020.10.11 |

