17

Javaコードからアクションバーのドロップシャドウを削除するにはどうすればよいですか?.

スタイルから削除すると、正常に機能します。

<style name="MyTheme" parent="Theme.Sherlock">
....
<item name="windowContentOverlay">@null</item>
<item name="android:windowContentOverlay">@null</item>
....
</style>

しかし、Javaコードから動的に削除して追加する必要があります。

4

7 に答える 7

12

windowContentOverlayプログラムで属性の値を設定する方法はありません。ただし、2 つの異なるテーマを定義できます。1 つは ActionBar の影が表示されるアクティビティ用で、もう 1 つはその他のアクティビティ用です。

<!-- Your main theme with ActionBar shadow. -->
<style name="MyTheme" parent="Theme.Sherlock">
    ....
</style>

<!-- Theme without ActionBar shadow (inherits main theme) -->
<style name="MyNoActionBarShadowTheme" parent="MyTheme">
    <item name="windowContentOverlay">@null</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

これで、次のアクティビティに設定できますAndroidManifest.xml

<!-- Activity with ActionBar shadow -->
<activity
    android:name=".ShadowActivity"
    android:theme="@style/MyTheme"/>

<!-- Activity without ActionBar shadow -->
<activity
    android:name=".NoShadowActivity"
    android:theme="@style/MyNoActionBarShadowTheme"/>

onCreate()または、メソッドでプログラムで適切なテーマを設定できます。

@Override
protected void onCreate(Bundle savedInstanceState) {
    setTheme(R.style.MyNoActionBarShadowTheme);
    super.onCreate(savedInstanceState);

    //...
}
于 2014-10-08T11:08:18.463 に答える
4

このような新しいスタイルを定義します。親が定義されていないことに注意してください。

<style name="ConOver" >    <<== no parent
    <item name="android:windowContentOverlay">@null</item>
</style>

これをコードの上に追加します。

// Add this line before setContentView() call
// Lets you add new attribute values into the current theme
getTheme().applyStyle(R.style.ConOver, true);

// Rest stays the same
于 2014-10-05T20:02:28.373 に答える
3

2 つのスタイルを宣言する ne には影があり、もう 1 つは影がありません。次に、マニフェストで、各アクティビティのテーマをそれぞれのテーマで定義します。例えば

<Activity 
android:theme="@style/ThemeShadow" ...>

...

<Activity 
android:theme="@style/ThemeNoShadow" ...>
于 2014-08-31T21:23:43.950 に答える
2

この記事を見つけました: Androidでボタンをクリックしてレイアウトランタイムのテーマを変更する

アクティビティのテーマを変更できますが、finish() を呼び出してアクティビティを再開します。だから、あなたが望むもののためにそれがうまくいくかどうかはわかりません。

import android.app.Activity;
import android.content.Intent;

public class Utils
{
     private static int sTheme;

     public final static int THEME_DEFAULT = 0;
     public final static int THEME_WHITE = 1;
     public final static int THEME_BLUE = 2;

      /**
       * Set the theme of the Activity, and restart it by creating a new Activity of the same type.
       */
      public static void changeToTheme(Activity activity, int theme)
      {
           sTheme = theme;
           activity.finish();

 activity.startActivity(new Intent(activity, activity.getClass()));

      }

      /** Set the theme of the activity, according to the configuration. */
      public static void onActivityCreateSetTheme(Activity activity)
      {
           switch (sTheme)
                {
                default:
           case THEME_DEFAULT:
               activity.setTheme(R.style.FirstTheme);
               break;
           case THEME_WHITE:
               activity.setTheme(R.style.SecondTheme);
               break;
           case THEME_BLUE:
               activity.setTheme(R.style.Thirdheme);
               break;
           }
      }
 }
于 2014-10-08T16:48:37.350 に答える