Android 透明应用

想要在应用中搞一个透明主题,可以在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 发展到3.0(大概)之后,可以使用fragment了,但是support v4 提供了1.6~3.0的fragment兼容,所以如果需要用兼容版的fragment,则需要继承support v4提供的FragmentActivity。而后一点点时间之后,3.0(大概)出现的ActionBar也被向前支持了,这次是出现在support v7里,如果需要使用兼容版的actionbar,则继承support v7提供的ActionBarActivity(它是继承FragmentActivity的)。再然后也就是去年年底到今年,5.0提供了很多很多新东西,于是support v7也更新了,出现了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

Understanding the Android Support Library


--------------------------------------------------------------------------------------------------------------------------------------------------------------------------


有关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中无透明主题,方式如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <!-- Base application theme. -->  
  2.     <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">  
  3.         <!-- Customize your theme here. -->  
  4.         <item name="colorPrimary">@color/colorPrimary</item>  
  5.         <item name="colorPrimaryDark">@color/theme</item>  
  6.         <item name="colorAccent">@color/colorAccent</item>  
  7.         <item name="android:background">@color/translate</item>  
  8.     </style>  
  9.     <!--全屏加透明-->  
  10.     <style name="TranslucentFullScreenTheme" parent="FullScreenTheme">  
  11.         <item name="android:windowBackground">@color/translate</item>  
  12.         <item name="android:windowIsTranslucent">true</item>  
  13.     </style>  
  14.     <!--全屏-->  
  15.     <style name="FullScreenTheme" parent="AppTheme">  
  16.         <item name="android:windowFullscreen">true</item>  
  17.         <item name="android:windowNoTitle">true</item>  
  18.     </style>  
  19.     <!--透明,有任务栏电量时间等-->  
  20.     <style name="NoTitleTranslucentTheme" parent="AppTheme">  
  21.         <item name="android:windowNoTitle">true</item>  
  22.         <item name="android:windowBackground">@color/translate</item>  
  23.         <item name="android:windowIsTranslucent">true</item>  
  24.     </style>  




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值