Java 如何在 Android 选择器中定义粗体?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19115056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to define bold in an Android selector?
提问by kramer65
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.xmlfile in drawable:
在我的 Android 应用程序中,我有几个单选按钮,它们应该具有不同的颜色并在选择时变为粗体。我设法通过在 drawable 中定义radio_pick_color.xml文件来获得不同的颜色:
<?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:
并在我的 main.xml 文件中引用此文件:
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.xmlin which I wanted to define the style like this:
我现在想对文本加粗也做同样的事情。所以我制作了另一个名为radio_picker_style.xml 的文件,我想在其中定义这样的样式:
<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.
不幸的是,eclipse 抱怨在包“android”中找不到属性“style”的资源标识符。我也尝试过 android:textStyle,但是在选择器项中它也不知道该android:textStyle
属性。
Does anybody know how I can get the selected radio button in bold?
有人知道如何以粗体显示选定的单选按钮吗?
==EDIT== The relevant part of my main.xml file:
==EDIT== 我的 main.xml 文件的相关部分:
<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.xmlwhich I tried to put in drawable folder, but which says "Attribute is missing the Android namespace prefix":
还有我试图放在 drawable 文件夹中的radio_picker_style.xml,但上面写着“属性缺少 Android 命名空间前缀”:
<?xml version="1.0" encoding="utf-8"?>
<style name="mystyle">
<item name="android:textColor">#ffffff</item>
<item name="android:textStyle">bold</item>
</style>
采纳答案by ozbek
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:
主活动.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:
布局/ 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]:
drawable/ radio_picker_color.xml [为了演示目的,我用“#0099CC”更改了您在问题中使用的颜色(它们非常微弱)]:
<?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.
希望这可以帮助。
回答by SHANK
Use
用
android:textStyle = "bold"
回答by Jitender Dev
You can do something like this
你可以做这样的事情
Create your own style e.g. mystyle.xml
创建您自己的样式,例如 mystyle.xml
<style name="mystyle">
<item name="android:textColor">#ffffff</item>
<item name="android:textStyle">bold</item>
</style>
Now call this in your selector like this
现在像这样在您的选择器中调用它
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="true"
style="@style/mystyle" />
</selector>