【问题标题】:How to define bold in an Android selector?如何在 Android 选择器中定义粗体?
【发布时间】:2013-10-01 11:31:37
【问题描述】:

在我的 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>

并在我的 main.xml 文件中引用此文件:

android:textColor="@drawable/radio_picker_color"

我现在想对文本加粗做同样的事情。所以我制作了另一个名为 radio_picker_style.xml 的文件,我想在其中定义如下样式:

<item android:state_checked="true"
       android:style="bold" /> 

不幸的是,Eclipse 抱怨在包“android”中找不到属性“style”的资源标识符。我也尝试过使用 android:textStyle,但在选择器项中它也不知道 android:textStyle 属性。

有人知道我怎样才能将选中的单选按钮变为粗体吗?

==编辑== 我的 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>

还有 radio_picker_style.xml 我试图放入 drawable 文件夹,但上面写着“属性缺少 Android 命名空间前缀”:

<?xml version="1.0" encoding="utf-8"?>
<style name="mystyle">  
    <item name="android:textColor">#ffffff</item>
    <item name="android:textStyle">bold</item>
</style>

【问题讨论】:

  • @shoerat - 这绝对不是你建议的问题的重复。我知道如何设置颜色,但是我没有成功地将文本加粗。你知道如何使选定的文本加粗吗?
  • 我看到的唯一可行的解​​决方案是在运行时这样做......

标签: java android android-selector


【解决方案1】:

样式没有选择器:使用一个会破坏构建/运行时,或者没有效果并被忽略。只有两种类型的选择器:用于颜色和用于可绘制对象。

因此,只有一种选择:动态应用粗体样式,可能通过侦听检查状态变化。

=== 示例 ===

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);
    }
}

布局/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 [出于演示目的,我用“#0099CC”更改了您在问题中使用的颜色(它们非常微弱)]: p>

<?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>

结果

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    你可以这样做

    创建自己的风格,例如我的样式.xml

    <style name="mystyle">  
        <item name="android:textColor">#ffffff</item>
        <item name="android:textStyle">bold</item>
    </style>
    

    现在像这样在你的选择器中调用它

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
          android:state_checked="true"
          style="@style/mystyle" />
    </selector>
    

    【讨论】:

    • 我创建了 radio_selected.xml 并在其中复制粘贴了您的代码,但它说“属性缺少 Android 命名空间前缀”,所以我将所有出现的“名称”更改为“android:name ",但它仍然说该属性缺少 android 命名空间前缀。此外,它还显示“在布局文件中发现意外的文本:#fff”。有什么想法我在这里做错了吗?
    • 发布完整的 xml
    • 我在我的问题中添加了更多代码。这能告诉你更多吗?
    【解决方案3】:

    使用

    android:textStyle = "bold"
    

    【讨论】:

    • 对不起,我应该提到当您在选择器项中时,这也不起作用。它还说它没有 textStyle 属性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-14
    • 2017-11-07
    • 2020-01-22
    相关资源
    最近更新 更多