7

私には2つの活動があります:

public class MainActivity extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
 }
 public void click(View view) {
  Intent intent= new Intent(this, TranslucentActivity.class);
  startActivity(intent);        
 }
}

public class TranslucentActivity extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  this.setTheme(android.R.style.Theme_Translucent);
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_translucent);
 }
}

レイアウトactivity_main

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android1="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:onClick="click" />
</RelativeLayout>

レイアウトactivity_translucent

<RelativeLayout 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"
    tools:context=".TranslucentActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Hello_world" />
</RelativeLayout>

マニフェスト

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.translucencytest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.translucencytest.MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.translucencytest.TranslucentActivity">
        </activity>
    </application>
</manifest>

MainActivityのボタンをクリックすると、透明な背景でTranslucentActivityが起動します。プラットフォーム4.0.3のAndroidエミュレーターではすべて正常に見えますが、2.3.3や4.2などの他のプラットフォームでは、透明ではなく黒の背景が表示されます。何が間違っている可能性がありますか?

PS私はマニフェストを使って活動のテーマを宣言したくありません。

4

2 に答える 2

11

setTheme()については多くの議論がありますが、setTheme()が期待どおりに機能しない理由は次のとおりです。

その議論の重要な点は、マニフェストにテーマを設定している間はsetThemeがうまく機能しないことです(Dianne Hackbornでさえ、setTheme()の方法よりもマニフェストの方法を使用することをお勧めします。上記の3番目のリンクを参照してください)。これは、背景の定義に関して特に当てはまります。エミュレーターまたはグラフィカルレイアウトエディターで取得した結果は、残念ながら現実の世界(実際のデバイスと言えば)に転送できません。したがって、4.0.3エミュレーターに表示されるものは、実際のデバイスでは同じではない可能性があります(すでに気付いたように;-)。

setThemeを使用してアクティビティのテーマを設定する特別な理由がない場合は、マニフェストを次のように変更することをお勧めします。

<activity android:name=".TranslucentActivity"
          android:theme="@android:style/Theme.Translucent"/>

setThemeを使用して、レイアウトの他の要素にテーマを設定することはできますが、透明なアクティビティやダイアログのようなアクティビティを作成する場合、他の実用的なソリューションは見つかりませんでした。

于 2013-03-18T14:00:26.370 に答える
-1
<activity android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:name="com.example.translucencytest.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:name="com.example.translucencytest.TranslucentActivity">
</activity>
于 2013-06-24T15:29:02.683 に答える