Android的如何使按钮上的文字加粗时pressed或聚焦按钮、加粗、文字、Android

2023-09-12 09:25:10 作者:内在

我想改变内部按钮上的文本要大胆时突出显示按钮或pressed。我目前使用一个XML文件来定义按钮并使用XML来改变它的外观时,pressed,但我想这样做没有使用图像。

 < XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目安卓state_focused =真
          机器人:STATE_ pressed =假
          机器人:可绘制=@可绘制/ reset_hover/>
    <项目安卓state_focused =真
          机器人:STATE_ pressed =真
          机器人:可绘制=@可绘制/ reset_hover/>
    <项目安卓state_focused =假
          机器人:STATE_ pressed =真
      机器人:可绘制=@可绘制/ reset_hover/>
    <项目机器人:可绘制=@可绘制/复位/>
< /选择器>
 

我尝试使用类似以下,但它似乎没有以往任何时候都被调用。

 最后按钮btn_reset =(按钮)findViewById(R.id.btn_reset);
    btn_reset.setOnClickListener(本);
    btn_reset.setOn(新OnFocusChangeListener(){
    @覆盖
    公共无效onFocusChange(视图V,布尔hasFocus){
        如果(hasFocus){btn_reset.setTypeface(空,Typeface.BOLD);}
        其他{btn_reset.setTypeface(空,Typeface.NORMAL);}
    }
   });
 

解决方案 android 怎么把button变成圆形

您可以创建自己的style.xml中定义的文本样式。在你的选择,你可以参考的风格。 style.xml

 <样式名称=myStyle的>
    <项目名称=机器人:TEXTSIZE> 9px< /项目>
    <项目名称=安卓重力> center_horizo​​ntal< /项目>
    <项目名称=机器人:文字颜色>#FFF< /项目>
    <项目名称=机器人:TEXTSTYLE>大胆< /项目>
< /风格>
 

而在你的选择

 <选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
<项目安卓state_focused =真
      机器人:STATE_ pressed =假
      风格=@风格/ myStyle的/> < /选择器>
 

I want to change the text inside a button to be bold when the button is highlighted or pressed. I currently use a xml file to define the button and use the XML to change how it looks when pressed but I would like to do this without using an image.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true"
          android:state_pressed="false" 
          android:drawable="@drawable/reset_hover" />
    <item android:state_focused="true" 
          android:state_pressed="true"
          android:drawable="@drawable/reset_hover" />
    <item android:state_focused="false" 
          android:state_pressed="true"
      android:drawable="@drawable/reset_hover" />
    <item android:drawable="@drawable/reset" />
</selector>

I tried using something like the following, but it doesn't seem to ever get called.

    final Button btn_reset = (Button) findViewById(R.id.btn_reset);
    btn_reset.setOnClickListener(this); 
    btn_reset.setOn(new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus){btn_reset.setTypeface(null, Typeface.BOLD);}
        else{btn_reset.setTypeface(null, Typeface.NORMAL);}
    }
   });

解决方案

You could create your own style.xml in which you define the text style. In your selector you can reference to the style. style.xml

<style name="myStyle">  
    <item name="android:textSize">9px</item>
    <item name="android:gravity">center_horizontal</item>
    <item name="android:textColor">#fff</item>
    <item name="android:textStyle">bold</item>
</style>

And in your selector

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
      android:state_pressed="false" 
      style="@style/myStyle" /> </selector>