Вопрос по android – Полупрозрачная активность, заполняющая весь экран
Я хотел бы иметь действие (2) с полупрозрачным аспектом над другим действием (1), выровненным в верхней части экрана (4).
Я попытался присвоить эти темы занятию № 2:
<code><style name="Theme.CustomDialog" parent="android:style/Theme.Dialog"> <item name="android:windowBackground">@android:color/black</item> </style> <style name="CustomTheme"> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:background">@android:color/transparent</item> <item name="android:windowIsFloating">true</item> <item name="android:windowNoTitle">true</item> </style> </code>
Но результат всегда 3.
Если я установлю<item name="android:windowIsFloating">false</item>
вCustomTheme
результат 2.
Кто-нибудь может сказать мне, как я могу получить 4? Спасибо!
UPDATE: Это макет моей деятельности 2:
<code><?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#0000"> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#FFFFFF"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Menu" android:layout_centerHorizontal="true"/> </RelativeLayout> </RelativeLayout> </code>
Если вы используетеAppCompatActivity
тогда вы должны использовать в качестве родителяTheme.AppCompat
в противном случае приложение может зависнуть или завершиться с ошибкой (java.lang.RuntimeException: Unable to start activity ComponentInfo ... Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.)
.
<style name="URTransparent" parent="Theme.AppCompat">
// Copied from Theme.Translucent
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:backgroundDimAmount">0.5</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:background">@android:color/transparent</item>
</style>
Наконец, эта тема работала, чтобы получить результат, подобный изображению № 4:
<style name="Theme.CustomTranslucent" parent="android:style/Theme.Translucent">
<item name="android:backgroundDimEnabled">true</item>
<item name="android:backgroundDimAmount">0.5</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:background">@android:color/transparent</item>
</style>
В моей деятельности 2 макет, я мог бы установитьandroid:background="@android:color/transparent"
или вообще не устанавливать какое-либо значение, чтобы оно работало.
Спасибо MikeIsrael и Veer за помощь.
Сначала выполните прозрачную тему:
<style name="Theme.Transparent" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
Назначьте прозрачную тему вашей деятельности в Манифесте:
<activity android:name=".MyActivity"
android:label="@string/app_name"
android:theme="@style/Theme.Transparent"/>
Создайте макет в соответствии с вашими требованиями и настройте представление контента вашей деятельности на этот макет.
Попробуйте следующий макет:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/transparent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:layout_alignParentTop="true">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Menu" android:layout_centerHorizontal="true"/>
</RelativeLayout>
</RelativeLayout>
Надеюсь, это полезно.
Я прочитал другие решения, но вот мое решение:
style.xml
<resources>
<style name="mytransparent.windowNoTitle" parent="android:Theme.Holo.Light">
<item name="android:windowNoTitle">true</item>
<item name="android:background">@android:color/transparent</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">@android:style/Animation</item>
</style>
<style name="mytransparent.windowTitle" parent="android:Theme.Holo.Light">
<item name="android:background">@android:color/transparent</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">@android:style/Animation</item>
</style>
AndroidManifest.xml
<activity
android:name=".LoginActivity"
android:theme="@style/mytransparent.windowTitle"
android:configChanges="orientation"
android:label="@string/title_activity_login"
android:screenOrientation="portrait" ></activity>
Я не уверен, как это сделать через xml, но это должно работать программно, попробуйте добавить это в свою деятельность (может быть, в основной вид деятельности).
//grab layout params
WindowManager.LayoutParams lp = this.getWindow().getAttributes();
//remove the dim effect
lp.dimAmount = 0;