在我的Android应用我有一对夫妇radion按钮应该得到一个不同的颜色,并成为在选择大胆。我设法在绘制定义的 radio_pick_color.xml 文件,以获得不同的颜色:
<?XML版本=1.0编码=UTF-8&GT?;<选择的xmlns:机器人=http://schemas.android.com/apk/res/android> <! - 检查 - > <项目的android:state_checked =真 机器人:颜色=#00FF15/> <! - 默认 - > <项目的android:颜色=#4000FF/>< /选择>
在我的main.xml文件中引用这个文件:
机器人:文字颜色=@绘制/ radio_picker_color
我现在要为具有文本加粗这样做。所以我做了另一个名为文件的 radio_picker_style.xml 中,我想这样定义的样式:
<项目的android:state_checked =真 机器人:风格=大胆/>
不幸的是日食,没有资源标识符可以为属性'风格'包中'机器人'可以找到投诉。我也试图与机器人:文字样式,但是从选择项中也并不知道的android:TEXTSTYLE
属性
有谁知道我可以大胆让选定的单选按钮?
==编辑==我的main.xml文件的相关部分:
< RadioGroup中 机器人:ID =@ + ID / radioGroup2 机器人:layout_width =FILL_PARENT 机器人:layout_height =WRAP_CONTENT 机器人:方向=横向> <单选 机器人:ID =@ + ID / option_1 机器人:layout_width =WRAP_CONTENT 机器人:layout_height =WRAP_CONTENT 安卓:检查=真 机器人:文字=@字符串/ option_1 机器人:文字颜色=@绘制/ radio_picker_color /> <单选 机器人:ID =@ + ID / option_2 机器人:layout_width =WRAP_CONTENT 机器人:layout_height =WRAP_CONTENT 机器人:文字=@字符串/ option_2 机器人:文字颜色=@绘制/ radio_picker_color />< / RadioGroup中>
以及 radio_picker_style.xml 我试图把文件夹绘制,但它说属性缺少的Android命名空间preFIX
<?XML版本=1.0编码=UTF-8&GT?;<样式名称=myStyle的> <项目名称=机器人:文字颜色>#FFFFFF< /项目> <项目名称=机器人:文字样式>大胆< /项目>< /风格>
解决方案
的样式没有选择:使用一个要么打破建立/运行或将不会产生影响,可以忽略。只有两种类型的选择:对颜色和绘图资源
所以,只有一个选择:申请大胆风格上飞,可能是,通过倾听来检查状态更改
。的 ===举例=== 的
MainActivity.java
包com.example.radiobuttontest;进口android.os.Bundle;进口android.app.Activity;进口android.graphics.Typeface;进口android.widget.CompoundButton;进口android.widget.RadioButton;进口android.widget.RadioGroup;公共类MainActivity扩展活动实现CompoundButton.OnCheckedChangeListener { 私人RadioGroup中mGroup2; @覆盖 保护无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState); 的setContentView(R.layout.activity_main); mGroup2 =(RadioGroup中)findViewById(R.id.radioGroup2); 单选按钮1 =(单选)findViewById(R.id.option_1); button1.setOnCheckedChangeListener(本); } @覆盖 保护无效onResume(){ super.onResume(); 单选检查=(单选)findViewById(mGroup2.getCheckedRadioButtonId()); checked.setTypeface(Typeface.DEFAULT_BOLD); } @覆盖 公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked){ buttonView.setTypeface(Typeface.DEFAULT_BOLD器isChecked:Typeface.DEFAULT?); }}
布局/ activity_main.xml中
<?XML版本=1.0编码=UTF-8&GT?;< RadioGroup中的xmlns:机器人=http://schemas.android.com/apk/res/android 机器人:ID =@ + ID / radioGroup2 机器人:layout_width =match_parent 机器人:layout_height =match_parent 机器人:方向=横向> <单选 机器人:ID =@ + ID / option_1 机器人:layout_width =WRAP_CONTENT 机器人:layout_height =WRAP_CONTENT 安卓:检查=真 机器人:文字=@字符串/ option_1 机器人:文字颜色=@绘制/ radio_picker_color/> <单选 机器人:ID =@ + ID / option_2 机器人:layout_width =WRAP_CONTENT 机器人:layout_height =WRAP_CONTENT 机器人:文字=@字符串/ option_2 机器人:文字颜色=@绘制/ radio_picker_color/>< / RadioGroup中>
绘制/ radio_picker_color.xml [您使用我改变了颜色(他们是非常微弱的)的问题以#0099CC为示范的目的] 的:
<?XML版本=1.0编码=UTF-8&GT?;<选择的xmlns:机器人=http://schemas.android.com/apk/res/android> <! - 检查 - > <项目的android:state_checked =真 机器人:颜色=#0099CC/> <! - 默认 - > <项目的android:颜色=#0099CC/>< /选择>
结果
希望这有助于。
In my Android app I've got a couple radion buttons which should get a different color and become bold upon selection. I managed to get the different color by defining a radio_pick_color.xml file in drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- checked -->
<item android:state_checked="true"
android:color="#00FF15" />
<!-- default -->
<item android:color="#4000FF" />
</selector>
and referencing this file in my main.xml file:
android:textColor="@drawable/radio_picker_color"
I now want to do the same for having the text bold. So I made another file called radio_picker_style.xml in which I wanted to define the style like this:
<item android:state_checked="true"
android:style="bold" />
Unfortunately eclipse complaints that no resource identifier can be found for attribute 'style' in package 'android'. I also tried with android:textStyle, but from within a selector item it also doesn't know the android:textStyle
attribute.
Does anybody know how I can get the selected radio button in bold?
==EDIT== The relevant part of my main.xml file:
<RadioGroup
android:id="@+id/radioGroup2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/option_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/option_1"
android:textColor="@drawable/radio_picker_color"
/>
<RadioButton
android:id="@+id/option_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/option_2"
android:textColor="@drawable/radio_picker_color"
/>
</RadioGroup>
And the radio_picker_style.xml which I tried to put in drawable folder, but which says "Attribute is missing the Android namespace prefix":
<?xml version="1.0" encoding="utf-8"?>
<style name="mystyle">
<item name="android:textColor">#ffffff</item>
<item name="android:textStyle">bold</item>
</style>
解决方案
The style has no selector: using one will either break the build/runtime or will have no effect and be neglected. There are only two types of selectors: for colors and for drawables.
So, there is only one option: apply bold style on the fly, possibly, by listening to check state changes.
=== Example ===
MainActivity.java:
package com.example.radiobuttontest;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Typeface;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class MainActivity extends Activity implements CompoundButton.OnCheckedChangeListener {
private RadioGroup mGroup2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGroup2 = (RadioGroup)findViewById(R.id.radioGroup2);
RadioButton button1 = (RadioButton)findViewById(R.id.option_1);
button1.setOnCheckedChangeListener(this);
}
@Override
protected void onResume() {
super.onResume();
RadioButton checked = (RadioButton)findViewById(mGroup2.getCheckedRadioButtonId());
checked.setTypeface(Typeface.DEFAULT_BOLD);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
buttonView.setTypeface(isChecked ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT);
}
}
layout/activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/radioGroup2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/option_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/option_1"
android:textColor="@drawable/radio_picker_color" />
<RadioButton
android:id="@+id/option_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/option_2"
android:textColor="@drawable/radio_picker_color" />
</RadioGroup>
drawable/radio_picker_color.xml [I changed colors you have used (they are very faint) in the question with "#0099CC" for demonstration purposes]:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- checked -->
<item android:state_checked="true"
android:color="#0099CC" />
<!-- default -->
<item android:color="#0099CC" />
</selector>
Result:
Hope this helps.