我在style.xml文件中自定义了一个按钮的样试:<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="NavButton">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">23dp</item>
<item name="android:paddingTop">2dp</item>
<item name="android:paddingBottom">2dp</item>
<item name="android:background">@drawable/nav_button</item>
<item name="android:focusable">true</item>
<item name="android:focusableInTouchMode">true</item>
</style>
        <style name="NavButton2">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">23dp</item>
<item name="android:paddingTop">2dp</item>
<item name="android:paddingBottom">2dp</item>
<item name="android:background">@drawable/nav_button</item>
<item name="android:focusable">true</item>
<item name="android:focusableInTouchMode">true</item>
</style>
</resources>
我想让用户选择不同的样式,在程序运行时动态的改变接按钮的样式,该如何做?

解决方案 »

  1.   

            <item android:state_focused="true" android:state_pressed="true"
    android:drawable="@drawable/nav_button" />
    <item android:state_focused="false" android:state_pressed="true"
    android:drawable="@drawable/nav_button" />
    <item android:state_focused="true" android:state_pressed="false"
    android:drawable="@drawable/nav_button" />
    这样写的
      

  2.   

    楼上的说的是按钮不同状态吧,有焦点按下,无焦点按下,有焦点没按下的状态显示不同的图...LZ的意思是换肤啊
    我也不知道...
    你可以把style写到theme里面,然后用setTheme()试试
      

  3.   

    楼主要改什么样式?
    两个Button样式是一样的应该可以只写一个吧!
      

  4.   

    有setTheme这个方法吗?
      应该只能用代码做了 但好像控件里面又没有直接设置主题的接口
    估计得用图源了哦!
    给每种样式设置一个标识 然后自己写一个接口 根据标识更改按钮的图源 :buttonImage = context.getResources().openRawResource(R.drawable.xxxx);
    drawImage = Drawable.createFromStream(buttonImage, "xxx");
    button.setBackgroundDrawable(drawImage); 然后每设置标识就调用一下改变图源的接口
      

  5.   

    setTheme()这个可以满足楼主需求吧
      

  6.   

    有个比较笨的想法,试试:
    在java文件中设置一个标记1代表A样式,2代表B样式;
    在接受切换样式的地方修改标志位,并且发送message进行处理;
    根据不同的样式,给相应的属性赋值,相反样式的属性赋空;
    这样按钮只表现出对应样式的属性
    刷新界面;
      

  7.   

    View好像没有setTheme()这样的方法吧.
    那么我们能不能换个思路,theme通过代码来实现呢?比如:<resources>
        <style name="NavButton">
            <item name="android:layout_width">wrap_content</item>
            <item name="android:layout_height">23dp</item>
            <item name="android:paddingTop">2dp</item>
            <item name="android:paddingBottom">2dp</item>
            <item name="android:background">@drawable/nav_button</item>
            <item name="android:focusable">true</item>
            <item name="android:focusableInTouchMode">true</item>
        </style>
    我们可以在代码里面写个方法r如: void setTheme(Button button){
       button.setBackgroundResource(XXX);
       button.setPadding(X, X, X, X);
       button.setFocusable(true);
       .....
        }当用户选择要切换theme的时候,我们直接给那个button来一个setTheme()不就可以了?
      

  8.   

    楼上的可以的,把样式分成多个文件中,通过setBackground实现