【问题标题】:Button background color is changed by portrait to landscape mode按钮背景颜色由纵向更改为横向模式
【发布时间】:2014-04-30 01:42:49
【问题描述】:

我是 Android 应用程序开发的新手。在我的应用程序中,如果我单击按钮,它将背景颜色从黑色更改为绿色。 如果我将模式从纵向更改为横向,则无需单击按钮即可将颜色从绿色更改为黑色。

我的要求是,必须通过将模式从纵向更改为横向或从横向更改为纵向来停止背景颜色更改。

请帮助我前进。

【问题讨论】:

    标签: android


    【解决方案1】:

    当屏幕方向改变时,方法onCreate()被调用。

    我猜你是在这个onCreate() 方法中调用Button.setBackgroundColor()...

    尝试用if(savedInstanceBundle == null){} 包围此呼叫,这样它只会在第一个onCreate() 呼叫时将颜色设置为黑色,而不会用于任何后续呼叫。

    【讨论】:

      【解决方案2】:

      在清单中的活动声明中使用android:configChanges="orientation"。像这样

      <activity android:name=".MyActivity"
                android:configChanges="orientation"
                android:label="@string/app_name">
      

      【讨论】:

        【解决方案3】:

        当您旋转设备时,onCreate() 方法再次调用以避免在下面的代码中使用

        android:configChanges="orientation"
        

        在活动的 Manifest.xml 中

        <activity
             android:name="com.sample.stackoverflow.MainActivity"
             android:configChanges="orientation"
             android:label="@string/app_name" />
        

        如果你想在方向改变时加载特定的布局:

        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            // use your setcontentView here
        }
        

        【讨论】:

        • 1.On button click only I set background for button.2.I have used the following code android:configChanges="orientation" in my manifest.xml file.3.I use different layout for Landscape layout-land 文件夹中的模式。
        • 所以你需要在变量中设置布尔值。这样你就可以从这个值中设置背景颜色。因为你使用不同的布局。所以这就是为什么变化没有反映
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-06-04
        • 1970-01-01
        • 2021-06-22
        • 2015-04-03
        相关资源
        最近更新 更多