Farbe des RadioButton -Punktes

G

Gast2

Gast
Hi,

weiß jemand wie man die Farbe des "aktiv"-Punktes von RadioButtons einstellen kann?

Bei google nix gefunden. :(
 

xehpuk

Top Contributor
Kommt aufs Look and Feel an. Mit Metal und Windows scheint es nicht zu gehen, Windows Classic hingegen schon.

Das Setzen von
Code:
RadioButton.foreground
ändert die Farbe des Punkts und der Schrift. Ruft man jedoch
Code:
setForeground()
auf, ändert sich nur noch die Schrift. So kann man es dann also machen:

Java:
public class RadioButtonColorTest {
	private static final ColorUIResource defaultRadioButtonForeground;
	
	static {
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
		} catch (final Exception e) {
			throw new Error(e);
		}
		defaultRadioButtonForeground = (ColorUIResource) UIManager.put("RadioButton.foreground", new ColorUIResource(Color.RED));
	}
	
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				final JFrame frame = new JFrame();
				final JRadioButton button = create();
				button.setText("Lorem ipsum");
				frame.add(button);
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.pack();
				frame.setVisible(true);
			}
		});
	}
	
	public static JRadioButton create() {
		final JRadioButton button = new JRadioButton();
		button.setForeground(defaultRadioButtonForeground);
		return button;
	}
}

Alternativ kann man
Code:
RadioButton.icon
setzen, um das Icon komplett selbst zu zeichnen.
 

schlingel

Gesperrter Benutzer
Wenn's um Android geht, wird dir hier geholfen: SO Post

Dort wird erklärt wie das funktioniert. Du musst das android:button Attribut der RadioButton-View auf dein Icon setzen.

[xml]
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomTheme" parent="android:Theme">
<item name="android:radioButtonStyle">@style/RadioButton</item>
</style>
<style name="RadioButton" parent="@android:style/Widget.CompoundButton.RadioButton">
<item name="android:button">@drawable/radio</item>
</style>
</resources>
[/xml]

Und radio muss ein Icon von dir sein, am besten ein Drawable mit den verschiedenen States:

[xml]
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_window_focused="false"
android:drawable="@drawable/radio_hover" />
<item android:state_checked="false" android:state_window_focused="false"
android:drawable="@drawable/radio_normal" />
<item android:state_checked="true" android:state_pressed="true"
android:drawable="@drawable/radio_active" />
<item android:state_checked="false" android:state_pressed="true"
android:drawable="@drawable/radio_active" />
<item android:state_checked="true" android:state_focused="true"
android:drawable="@drawable/radio_hover" />
<item android:state_checked="false" android:state_focused="true"
android:drawable="@drawable/radio_normal_off" />
<item android:state_checked="false" android:drawable="@drawable/radio_normal" />
<item android:state_checked="true" android:drawable="@drawable/radio_hover" />
</selector>
[/xml]
 

Neue Themen


Oben