반응형
프로그래밍 방식으로 설정됨 '?선택 가능Android 보기의 'Item background'
xml에서, 나는 종종 이것을 에뮬레이트하기 위해 합니다.onClick효과:
<android.support.v7.widget.CardView
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?selectableItemBackground">
...
</android.support.v7.widget.CardView>
접근할 수 있는 방법이 있습니까??selectableItemBackground자바로?
사용할 수 있는 appcat의 경우,
TypedValue outValue = new TypedValue();
getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
cardView.setBackgroundResource(outValue.resourceId);
Kotlin과 함께 일하는 사람들을 위해, 다음은 Ripple on Android View 유형을 추가하는 몇 가지 확장 기능입니다.
private fun View.addRipple() = with(TypedValue()) {
context.theme.resolveAttribute(android.R.attr.selectableItemBackground, this, true)
setBackgroundResource(resourceId)
}
private fun View.addCircleRipple() = with(TypedValue()) {
context.theme.resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, this, true)
setBackgroundResource(resourceId)
}
저도 같은 해결책을 찾고 있었습니다.질문한 질문에 좀 더 적합하도록 이 답변을 약간 바꿨습니다.컨스트럭터에서 다음 코드를 호출합니다.
private void setClickableAnimation(Context context)
{
TypedValue outValue = new TypedValue();
context.getTheme().resolveAttribute(
android.R.attr.selectableItemBackground, outValue, true);
setForeground(getDrawable(context, outValue.resourceId));
}
제가 사용하고 있는 코틀린의 경우
binding.yourCoolView.apply {
background = with(TypedValue()) {
context.theme.resolveAttribute(
androidx.appcompat.R.attr.selectableItemBackground, this, true)
ContextCompat.getDrawable(context, resourceId)
}
}
Kotlin을 사용하는 사람들을 위해, 이것은 @Nicolas 답변의 확장된 버전입니다.사용하시는 경우CardView, 당신은 바꿀 필요가 있습니다.foreground, 아닌background.
코드
fun View.addBackgroundRipple() = with(TypedValue()) {
context.theme.resolveAttribute(android.R.attr.selectableItemBackground, this, true)
setBackgroundResource(resourceId)
}
fun View.addBackgroundCircleRipple() = with(TypedValue()) {
context.theme.resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, this, true)
setBackgroundResource(resourceId)
}
fun View.addForegroundRipple() = with(TypedValue()) {
context.theme.resolveAttribute(android.R.attr.selectableItemBackground, this, true)
foreground = ContextCompat.getDrawable(context, resourceId)
}
fun View.addForegroundCircleRipple() = with(TypedValue()) {
context.theme.resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, this, true)
foreground = ContextCompat.getDrawable(context, resourceId)
}
사용.
// Background ripple
linearLayout.addBackgroundRipple()
// Foreground ripple
cardView.addForegroundRipple()
당신은 그것을 다음과 같이 참조하십시오.
android.R.attr.selectableItemBackground
@Wirling 답변을 바탕으로 전경을 사용하여 색상과 파급 효과를 모두 설정할 수 있습니다.
참고: 포그라운드에는 안드로이드 API 레벨 23(M) 이상이 필요합니다.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
TypedValue outValue = new TypedValue();
getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
view.setForeground(getDrawable(getContext(), outValue.resourceId));
}
아래 코드를 사용해 보십시오.
int[] attrs = new int[]{R.attr.selectableItemBackground};
TypedArray typedArray = context.obtainStyledAttributes(attrs);
int backgroundResource = typedArray.getResourceId(0, 0);
cardView.setBackgroundResource(backgroundResource);
cardView.setClickable(true);
typedArray.recycle();
조각에서:
TypedValue outValue = new TypedValue();
requireContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
cardView.setBackgroundResource(outValue.resourceId);
컨스트럭터에서 컨텍스트 또는 동일한 조각을 선언하는 어댑터에서:
TypedValue outValue = new TypedValue();
fragment.requireContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
holder.cardView.setBackgroundResource(outValue.resourceId);
언급URL : https://stackoverflow.com/questions/37987732/programmatically-set-selectableitembackground-on-android-view
반응형
'programing' 카테고리의 다른 글
| C에서 함수 호출 연산자의 연관성 (0) | 2023.10.10 |
|---|---|
| MySQL 행에서 선행 및 후행 견적을 자르는 방법은 무엇입니까? (0) | 2023.10.10 |
| 선택한 필드에 없는 필드 선택 - MySQL (0) | 2023.10.10 |
| SQLPLUS를 통한 Oracle 데이터베이스 연결 (0) | 2023.10.10 |
| 타이프스크립트에서 npm 모듈을 소비하는 방법은? (0) | 2023.10.10 |