在Android 2.3.3或4.2上以编程方式设置时,半透明主题不起作用

在Android 2.3.3或4.2上以编程方式设置时,半透明主题不起作用,第1张

概述我有两个活动:publicclassMainActivityextendsActivity{@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}publicvoidclick(Viewview){Intentintent=new

我有两个活动:

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,我得到黑色背景而不是透明.可能有什么不对?

附:我不想使用manifest来声明活动的主题.

解决方法:

关于settheme()的讨论很多,或者说为什么settheme()没有按预期工作:

> https://code.google.com/p/android/issues/detail?id=4394
> https://code.google.com/p/android/issues/detail?id=3793
> https://groups.google.com/forum/?fromgroups=#!topic/android-developers/vSZHsVWUCqk
> Why getApplicationContext().setTheme() in a Activity does not work?

这个讨论的典型特征是settheme在清单中设置主题时效果不佳(甚至Dianne Hackborn建议使用清单方式而不是settheme()方式,请参见上面的第三个链接).
在定义背景时尤其如此.
遗憾的是,您在模拟器或图形布局编辑器中获得的结果无法传输到现实世界(说实际设备).因此,您在4.0.3仿真器中看到的内容在真实设备上可能并不相同(正如您已经注意到的那样;-).

如果没有特别的理由使用settheme来主题你的Activity那么我建议你改变你的清单:

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

您仍然可以使用settheme来主题布局的其他元素,但在创建透明或类似对话的活动时,我还没有找到任何其他有用的解决方案.

总结

以上是内存溢出为你收集整理的在Android 2.3.3或4.2上以编程方式设置时,半透明主题不起作用全部内容,希望文章能够帮你解决在Android 2.3.3或4.2上以编程方式设置时,半透明主题不起作用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1097395.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存