【问题标题】:How to save the style of button and retrive it later?如何保存按钮的样式并在以后检索它?
【发布时间】:2014-03-17 17:14:44
【问题描述】:

我需要存储按钮的当前样式,以便无论何时将活动发送到后台或销毁并重新启动按钮都保留上次的样式。我知道如何保存和检索文本视图的信息,但我不知道如何为按钮样式执行此操作,基本上我对如何检索它感到更加困惑。 这是我设置按钮样式的代码:

case R.id.darkorange:
          for (Button currentButton : buttons) {
                currentButton.setBackgroundResource(R.drawable.darkorange);
            }

现在橙色可以是任何紫色、红色、蓝色或绿色。我如何保存有关按钮的这些信息,并在以后重新创建活动时检索它。

【问题讨论】:

  • 如果所有内容都是可绘制的,只需将 R.drawable.darkorange 存储在您的 sharedPreferences 中,它是一个整数
  • 谢谢,我对 R.drawable.darkorange 是整数感到困惑。现在我可以解决了。
  • 再问一个问题,因为颜色可以是上面列出的任何一种我如何从按钮中检索特定的颜色。

标签: java android android-layout


【解决方案1】:

这样做:

res/drawable/solid.xml:

 <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <gradient
        android:startColor="#0078a5"
        android:endColor="#00adee"
        android:angle="90"/>
    <padding android:left="15dp"
        android:top="1dp"
        android:right="15dp"
        android:bottom="1dp" />
    <stroke
        android:width="1dp"
        android:color="#0076a3" />
    <corners android:radius="8dp" />
</shape>

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<Button
    android:id="@+id/button1"
    style="@style/NiceButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:text="Colors"/>

   </LinearLayout>

res/values/strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Buttons</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Color</string>

</resources>

res/values/styles.xml:

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>
 <style name="NiceButton" parent="@android:style/Widget.Button">
    <item name="android:gravity">center_vertical|center_horizontal</item>
    <item name="android:textColor">#FFFFFF</item>
    <item name="android:background">@drawable/solid</item>
    <item name="android:textSize">16sp</item>
    <item name="android:textStyle">bold</item>
    <item name="android:focusable">true</item>
    <item name="android:clickable">true</item>
</style>
</resources>

输出:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-27
    • 1970-01-01
    • 2013-06-19
    • 2015-09-19
    相关资源
    最近更新 更多