라디오 단추의 원 색 변경
프로젝트 중 하나에서 RadioButton의 원 색상을 변경하고 싶은데 어떤 속성을 설정해야 하는지 이해할 수 없었습니다.배경색이 검은색이라 보이지 않습니다.원의 색상을 흰색으로 설정하고 싶습니다.
버튼 Tint 색상을 설정하는 것이 더 간단합니다(API 레벨 21 이상에서만 작동).
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radio"
android:checked="true"
android:buttonTint="@color/your_color"/>
values/colors.xml 파일에 색상을 입력합니다. 이 경우 빨간색으로 표시됩니다.
<color name="your_color">#e75748</color>
결과:
코드(API 21 이상)로 실행하려는 경우:
if(Build.VERSION.SDK_INT >= 21)
{
ColorStateList colorStateList = new ColorStateList(
new int[][]
{
new int[]{-android.R.attr.state_enabled}, // Disabled
new int[]{android.R.attr.state_enabled} // Enabled
},
new int[]
{
Color.BLACK, // disabled
Color.BLUE // enabled
}
);
radio.setButtonTintList(colorStateList); // set the color tint list
radio.invalidate(); // Could not be necessary
}
업데이트:
대신 이것을 사용합니다.
<android.support.v7.widget.AppCompatRadioButton android:id="@+id/rbtn_test" android:layout_width="wrap_content" android:layout_height="wrap_content" app:buttonTint="@color/primary" />
그런 다음 이 줄을 부모 레이아웃에 추가하거나 Android Studio에서 +를 추가하여 자동으로 추가합니다.
xmlns:app="http://schemas.android.com/apk/res-auto"
최소 예제는 다음과 같습니다.
<?xml version="1.0" encoding="utf-8"?>
<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"
android:orientation="vertical">
<android.support.v7.widget.AppCompatRadioButton
android:id="@+id/rbtn_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:buttonTint="@color/primary" />
</LinearLayout>
- 과 같이 : 프그램에합불니야다러같.
AppCompatRadioButton radioButton = (AppCompatRadioButton) view.findViewById(R.id.rbtn_test);
기본적으로 이러한 패턴은 AppCompatCheckBox, AppCompatButton 등과 같은 모든 AppCompact 유형에 적용할 수 있습니다.
이전 답변:
아래 안드로이드 API 21을 지원하기 위해 AppCompatRadioButton을 사용할 수 있습니다.사용할 경우setSupportButtonTintList
색을 바꾸는 방법.라디오 버튼을 만들기 위한 코드 조각입니다.
AppCompatRadioButton rb;
rb = new AppCompatRadioButton(mContext);
ColorStateList colorStateList = new ColorStateList(
new int[][]{
new int[]{-android.R.attr.state_checked},
new int[]{android.R.attr.state_checked}
},
new int[]{
Color.DKGRAY
, Color.rgb (242,81,112),
}
);
rb.setSupportButtonTintList(colorStateList);
API 19에서 테스트한 결과:
자세한 내용은 Android 참조 링크를 참조하십시오.
<android.support.v7.widget.AppCompatRadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:buttonTint="@color/Color" />
이것은 post 21 뿐만 아니라 API pre 21에서도 작동하고 있습니다.
의 신의에서.styles.xml
키워드:
<!-- custom style -->
<style name="radionbutton"
parent="Base.Widget.AppCompat.CompoundButton.RadioButton">
<item name="android:button">@drawable/radiobutton_drawable</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
XML의 라디오 버튼은 다음과 같아야 합니다.
<RadioButton
android:layout_width="wrap_content"
style="@style/radionbutton"
android:checked="false"
android:layout_height="wrap_content"
/>
할 를 것입니다.radiobutton_drawable.xml
의 신의에drawable folder
여기에 입력해야 할 내용이 있습니다.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/radio_unchecked" android:state_checked="false" android:state_focused="true"/>
<item android:drawable="@drawable/radio_unchecked" android:state_checked="false" android:state_focused="false"/>
<item android:drawable="@drawable/radio_checked" android:state_checked="true" android:state_focused="true"/>
<item android:drawable="@drawable/radio_checked" android:state_checked="true" android:state_focused="false"/>
</selector>
radio_unchecked.xml 파일:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke android:width="1dp" android:color="@color/colorAccent"/>
<size android:width="30dp" android:height="30dp"/>
</shape>
radio_checked.xml 파일:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<stroke android:width="1dp" android:color="@color/colorAccent"/>
<size android:width="30dp" android:height="30dp"/>
</shape>
</item>
<item android:top="5dp" android:bottom="5dp" android:left="5dp" android:right="5dp">
<shape android:shape="oval">
<solid android:width="1dp" android:color="@color/colorAccent"/>
<size android:width="10dp" android:height="10dp"/>
</shape>
</item>
</layer-list>
교체하기만 하면 됩니다.@color/colorAccent
당신이 선택한 색깔로.
styles.xml 파일에 사용자 지정 스타일을 선언합니다.
<style name="MyRadioButton" parent="Theme.AppCompat.Light"> <item name="colorControlNormal">@color/indigo</item> <item name="colorControlActivated">@color/pink</item> </style>
Android:테마 속성을 통해 라디오 버튼에 이 스타일을 적용합니다.
<RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="Radio Button" android:theme="@style/MyRadioButton"/>
이 확장되는 에만.AppCompatActivity
.
API 21에 의거하여
사용자 지정 스타일 라디오 단추 만들기:
File style.xml
<style name="RadioButton" parent="Theme.AppCompat.Light">
<item name="colorAccent">@color/green</item>
<item name="android:textColorSecondary">@color/mediumGray</item>
<item name="colorControlNormal">@color/red</item>
</style>
레이아웃에서 다음 테마를 사용합니다.
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/RadioButton" />
API 21 이상의 경우
버튼을 사용하면 됩니다.
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="@color/green" />
XML의 스타일을 사용하여 라디오 단추의 선택되지 않은 상태와 선택된 상태의 색상을 변경할 수 있습니다.
<RadioButton
android:id="@+id/rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/RadioButtonStyle" />
style.xml에서
<style name="RadioButtonStyle" parent="Theme.AppCompat.Light">
<item name="colorAccent">@android:color/white</item>
<item name="android:textColorSecondary">@android:color/white</item>
</style>
이 스타일로 원하는 색상을 설정할 수 있습니다.
다음 코드를 사용해야 합니다.
<android.support.v7.widget.AppCompatRadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radiobutton1"
app:buttonTint="@color/black" />
용사를 합니다.app:buttonTint
에 android:buttonTint
그리고 또한android.support.v7.widget.AppCompatRadioButton
에 Radiobutton
!
을 합니다.buttonTint
소유물.예를들면,android:buttonTint="#99FF33"
.
다음과 같이 colorControlNormal을 재정의하면 되는 경우가 있습니다.
<style name="RadioButtonStyle" parent="AppTheme">
<item name="colorControlNormal">@color/pink</item>
<item name="colorAccent">@color/colorPrimary</item>
<item name="android:textColorSecondary">@color/black</item>
</style>
다음과 같은 버튼이 표시됩니다.
colorControlNormal은 선택되지 않은 상태에 사용되고 colorAccent는 선택된 상태에 사용됩니다.
저는 이렇게 짧게 만들었습니다(API pre 21과 post 21에서 작업).
XML의 라디오 버튼은 다음과 같아야 합니다.
<RadioButton android:id="@+id/radioid"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:button="@drawable/radiodraw" />
radiodraw.xml 파일:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" >
<shape android:shape="oval" >
<stroke android:width="1dp" android:color="#000"/>
<size android:width="30dp" android:height="30dp"/>
<solid android:color="@android:color/transparent"/>
</shape>
</item>
<item android:state_checked="true">
<layer-list>
<item>
<shape android:shape="oval">
<stroke android:width="1dp" android:color="#000"/>
<size android:width="30dp" android:height="30dp"/>
<solid android:color="@android:color/transparent"/>
</shape>
</item>
<item android:top="5dp" android:bottom="5dp" android:left="5dp" android:right="5dp">
<shape android:shape="oval">
<solid android:width="1dp" android:color="#000"/>
<size android:width="10dp" android:height="10dp"/>
</shape>
</item>
</layer-list>
</item>
</selector>
선택하지 않은 상태를 그리려면 투명한 색상을 추가해야 합니다. 그렇지 않으면 검은색 타원형이 그려집니다.
실행 중지, 선택 및 실행 상태를 변경하려는 사용자는 다음 단계를 수행할 수 있습니다.
<!-- Or androidX radio button or material design radio button -->
<android.support.v7.widget.AppCompatRadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="@color/black"
android:text="Radiobutton1"
app:buttonTint="@color/radio_button_color" />
그런 다음 color res 폴더에서 "radio_button_color.xml" 파일을 만듭니다.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/yellow900" android:state_selected="true" />
<item android:color="@color/yellow800" android:state_checked="true" />
<item android:color="@color/gray800" android:state_enabled="false" />
<item android:color="@color/yellow800" android:state_enabled="true" />
</selector>
XML 속성은 다음과 같습니다.
android:buttonTint="yourcolor"
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radio"
android:buttonTint="@color/my_color"/>
모든 버튼의 색상, 원 상자 및 중앙 체크 표시가 변경됩니다.
사용하기만 하면 됩니다.android:buttonTint="@color/colorPrimary"
<RadioButton> 태그의 속성입니다.
RadioButton은 기본적으로 res/values/colors.xml 파일의 colorAccent 색상을 사용합니다.해당 파일로 이동하여 값을 변경합니다.
<color name="colorAccent">#3F51B5</color>
당신이 원하는 색으로.
XML을 에서 이 을 사용할 수 .android:buttonTint
속성:
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radio"
android:checked="false"
android:padding="5dp"
android:buttonTint="@color/radio_color"/>
Java에서 다음을 사용하여 이러한 방식으로 수행할 수 있습니다.android:buttonTint
:
// RadioButton ColorStateList
ColorStateList colorStateList = new ColorStateList(
new int[][]{
new int[]{-android.R.attr.state_checked}, // Unchecked
new int[]{android.R.attr.state_checked} // Checked
},
new int[]{
DataUtils.getColorResource(mContext, R.color.colorBlack), // Unchecked
DataUtils.getColorResource(mContext, R.color.colorPrimary) // Checked
}
);
RadioButton radio = findViewById(R.id.radio);
radio.setButtonTintList(colorStateList);
그리기 가능한 파일 my_drawable_button_color_drawable.xml 생성
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@color/text_app_color"/>
<item android:state_checked="true" android:color="@color/text_app_color"/>
<item android:color="@color/gray"/> </selector>
style.xml 파일에 style 추가
<style name="AppRadioAppStyle" parent="Widget.AppCompat.CompoundButton.RadioButton">
<item name="android:textColor">@drawable/my_compound_button_color_selector</item>
<item name="drawableTint">@drawable/my_compound_button_color_selector</item>
</style>
레이아웃 파일
<RadioButton
android:id="@+id/radio1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
android:gravity="center_horizontal|bottom"
android:background="@drawable/app_border_0"
android:padding="@dimen/_15sdp"
android:text="@string/no"
android:fontFamily="@font/poppins_medium"
style="@style/AppRadioAppStyle"
android:layout_marginStart="@dimen/_10sdp"/>
라디오 버튼에 안드로이드를 추가해야 합니다. button="@filename"
가장 쉬운 방법은 변화하는 것입니다.colourAccent
에 색을 입히다.values->colours.xml
그러나 텍스트 커서 색상 편집 등 다른 작업도 변경할 수 있습니다.
< color name="colorAccent">#75aeff</color >
저는 이런 문제가 있었습니다.이고 배경 않는 버튼이 , 합니다.android:buttonTint
각각의 속성.가장 좋은 해결책은 styles.xml 파일에서 부모 테마를 변경하는 것입니다.
나는 변했어요
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
로.
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
그래서 라디오 버튼의 원은 옅은 회색 색조가 되었고 이제는 검은색 배경에서도 볼 수 있습니다.
다음은 mystyle.xml 파일입니다.
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
라디오 단추 색을 프로그래밍 방식으로 변경하려면 다음을 사용합니다.
yourradio button name.buttonDrawable?.setColorFilter(Color.parseColor( color_value), PorterDuff.Mode.SRC_ATOP)
가지고 계신다면,android:buttonTint
그것은 작동하지 않을 것이고 당신은 그것을 바꿔야 합니다.app:buttonTint
나는 안드로이드x로 업그레이드한 후에 이것을 해야만 했습니다.
안드로이드 대신 app:buttonTint를 사용합니다. buttonTint 다음과 같은 Tint:
<com.google.android.material.radiobutton.MaterialRadioButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="English"
android:checked="true"
app:buttonTint="#FF0000"
android:textAppearance="@style/TextAppearance.Material3.TitleSmall"
android:layout_marginHorizontal="16dp"
android:layoutDirection="rtl"
/>
또는
<RadioButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="English"
android:checked="true"
app:buttonTint="#FF0000"
android:textAppearance="@style/TextAppearance.Material3.TitleSmall"
android:layout_marginHorizontal="16dp"
android:layoutDirection="rtl"
/>
이 코틀린 확장자
fun RadioButton.setLangRadioColor(isCheck: Boolean) {
val color = if (isCheck) {
intArrayOf(
ContextCompat.getColor(rootView.context, R.color.light_red),
ContextCompat.getColor(rootView.context, R.color.light_red)
)
} else {
intArrayOf(
ContextCompat.getColor(rootView.context, R.color.sortRadioUnselectColor),
ContextCompat.getColor(rootView.context, R.color.sortRadioUnselectColor)
)
}
val colorStateList = ColorStateList(
arrayOf(
intArrayOf(-android.R.attr.state_enabled), // disabled
intArrayOf(android.R.attr.state_enabled) // enabled
),
color
)
this.buttonTintList = colorStateList
}
선택된 상태와 선택되지 않은 상태에 따라 다른 색상을 사용하려면 다음을 사용하십시오.
파일 @color/radio_button
-
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/grey" android:state_enabled="false" />
<item android:color="@color/grey" android:state_checked="false" />
<item android:color="@color/green" android:state_enabled="true" />
<item android:color="@color/green" android:state_checked="true" />
</selector>
그런 다음 이렇게 사용합니다.
<androidx.appcompat.widget.AppCompatRadioButton
android:id="@+id/radio_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="@color/radio_button" />
**교체할 사항을 기억하십시오.android:buttonTint
와 함께app:buttonTint
테마를 사용하는 경우.재료 3.낮의 밤
클릭한 라디오 단추와 클릭하지 않은 라디오 단추의 색을 다르게 설정하려면 다음을 사용합니다.
android:buttonTint="@drawable/radiobutton"
radiobutton.xml 파일의 XML 내용은 다음과 같습니다.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#1E88E5"/>
<item android:state_checked="true" android:color="#00e676"/>
<item android:color="#ffffff"/>
</selector>
@jh314가 맞습니다.
AndroidManifest.xml 파일에서
<application
android:allowBackup="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/AppTheme"></application>
file style.xml에서:
<!-- Application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorAccent">@color/red</item>
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
항목 이름은 colorAccent여야 합니다.응용프로그램의 위젯 기본 색상을 결정합니다.
하지만 코드에서 색상을 변경하고 싶다면 @aknay의 대답이 맞을 수 있습니다.
언급URL : https://stackoverflow.com/questions/17120199/change-the-circle-color-of-radio-button
'source' 카테고리의 다른 글
파워셸 스크립트를 사용하여 두 폴더를 동기화하는 방법 (0) | 2023.08.22 |
---|---|
MariaDB: 대량 삽입 작업을 활성화하는 방법 (0) | 2023.08.22 |
Ubuntu에서 기본 데이터 디렉토리 MariaDB를 변경한 후 테스트 파일을 생성할 수 없음 (0) | 2023.08.22 |
iOS: NSUserDefaults에서 부울 사용 (0) | 2023.08.22 |
옵션이 선택되었는지 확인하는 방법은 무엇입니까? (0) | 2023.08.22 |