想要在应用中搞一个透明主题,可以在Manifest中更改android:theme
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"><!-- 改这里 -->
<activity android:name=".MainActivity">
并让MainActivity继承Activity而不是AppCompatActivity
public class MainActivity extends Activity { //继承Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
这样开启的应用就变成透明主题的了,注意屏幕中间的HelloWorld,字还在表示应用已开启。
如果不用Activity,会报异常 java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
------------------------------------------------------------------------------------------------------------------------------
但是为什么要继承Activity而不是AppCompatActivity呢?
Activity is the baseline. Every activity inherits from Activity, directly or indirectly.
FragmentActivity is for use with the backport of fragments found in the support-v4 and support-v13 libraries. The native implementation of fragments was added in API Level 11, which is higher than your proposed minSdkVersion values. The only reason why you would need to consider FragmentActivity specifically is if you want to use nested fragments (a fragment holding another fragment), as that was not supported in native fragments until API Level 17.
AppCompatActivity is from the appcompat-v7 library. Principally, this offers a backport of the action bar. Since the native action bar was added in API Level 11, you do not need AppCompatActivity for that. However, current versions of appcompat-v7 also add a limited backport of the Material Design aesthetic, in terms of the action bar and various widgets. There are pros and cons of using appcompat-v7, well beyond the scope of this specific Stack Overflow answer.
ActionBarActivity is the old name of the base activity from appcompat-v7. For various reasons, they wanted to change the name. Unless some third-party library you are using insists upon an ActionBarActivity, you should prefer AppCompatActivity over ActionBarActivity.
So, given your minSdkVersion in the 15-16 range:
-
If you want the backported Material Design look, use AppCompatActivity
-
If not, but you want nested fragments, use FragmentActivity
-
If not, use Activity
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
有关AppCompatActivity透明化处理的失败探索历程
http://blog.csdn.NET/lq2007lq/article/details/52606797
当Activity继承于AppCompatActivity时,只能使用Theme.AppCompat下的主题。而这些主题并没有Translucent.提示错误:
Java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
可以采用自定义主题来弥补AppCompat中无透明主题,方式如下: