Android에서 투명한 활동을 어떻게 생성합니까?
다른 활동 위에 투명한 활동을 만들고 싶습니다.
이것을 어떻게 할 수 있습니까?
res/values/styles.xml
파일에 다음 스타일을 추가 합니다 (없는 경우 새로 만듭니다.) 다음은 전체 파일입니다.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<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>
</resources>
(값 @color/transparent
은 #00000000
내가 res/values/color.xml
파일에 넣은 색상 값 입니다 . @android:color/transparent
이후 Android 버전 에서도 사용할 수 있습니다 .)
그런 다음 활동에 스타일을 적용합니다. 예를 들면 다음과 같습니다.
<activity android:name=".SampleActivity" android:theme="@style/Theme.Transparent">
...
</activity>
다음과 같이 진행됩니다.
<activity android:name=".usual.activity.Declaration" android:theme="@android:style/Theme.Translucent.NoTitleBar" />
styles.xml에서 :
<style name="Theme.AppCompat.Translucent" parent="Theme.AppCompat.NoActionBar">
<item name="android:background">#33000000</item> <!-- Or any transparency or color you need -->
<item name="android:windowNoTitle">true</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=".WhateverNameOfTheActivityIs"
android:theme="@style/Theme.AppCompat.Translucent">
...
</activity>
다음과 같이 매니페스트에서 활동을 선언하십시오.
<activity
android:name=".yourActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"/>
레이아웃에 투명한 배경을 추가합니다.
프로젝트의 Android 매니페스트 파일에서 투명하게 만들려는 활동에 반투명 테마를 할당합니다.
<activity
android:name="YOUR COMPLETE ACTIVITY NAME WITH PACKAGE"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
나는 새로운 안드로이드 개발자이기 때문에 이것에 약간 추가하고 싶었습니다. 받아 들여지는 대답은 훌륭하지만 문제가 발생했습니다. colors.xml 파일에 색상을 추가하는 방법을 잘 모르겠습니다. 수행 방법은 다음과 같습니다.
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="class_zero_background">#7f040000</color>
<color name="transparent">#00000000</color>
</resources>
원래 colors.xml 파일에는 "drawable"태그가 있습니다.
<drawable name="class_zero_background">#7f040000</drawable>
그래서 저는 색상에 대해서도 그렇게했지만 "@ color /"참조가 XML에서 "color"태그를 찾는 것을 의미한다는 것을 이해하지 못했습니다. 다른 사람을 돕기 위해 이것도 언급해야한다고 생각했습니다.
2.3.3 android:theme="@android:style/Theme.Translucent"
에서 매니페스트의 활동 태그를 추가하여 달성했습니다 .
낮은 버전에 대해 잘 모르겠습니다 ...
제 경우에는 일부 조건에 따라 Java의 런타임에 테마를 설정해야합니다. 그래서 스타일로 하나의 테마를 만들었습니다 (다른 답변과 유사).
<?xml version="1.0" encoding="utf-8"?>
<resources>
<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>
</resources>
Then in Java I applied it to my activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
String email = getIntent().getStringExtra(AppConstants.REGISTER_EMAIL_INTENT_KEY);
if (email != null && !email.isEmpty())
{
// We have the valid email ID, no need to take it from user, prepare transparent activity just to perform bg tasks required for login
setTheme(R.style.Theme_Transparent);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
else
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dummy);
}
Remember one Important point here: You must call the setTheme()
function before super.onCreate(savedInstanceState);
. I missed this point and stucked for 2 hours, thinking why my theme is not reflected at run time.
Just let the activity background image be transparent. Or add the theme in the XML file:
<activity android:name=".usual.activity.Declaration" android:theme="@android:style/Theme.Translucent.NoTitleBar" />
In the onCreate function, below the setContentView, add this line:
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
The easiest way that I have found is to set the activity's theme in the AndroidManifest to android:theme="@android:style/Theme.Holo.Dialog"
.
Then in the activity's onCreate method, call getWindow().setBackgroundDrawable(new ColorDrawable(0));
.
For dialog activity I use this:
getWindow().getDecorView().setBackgroundResource(android.R.color.transparent);
But you also need to set your main View in the activity to invisible. Otherwise the background will be invisible while all views in it will be visible.
in addition to the above answers:
to avoid android Oreo related crash on activity
<style name="AppTheme.Transparent" parent="@style/Theme.AppCompat.Dialog">
<item name="windowNoTitle">true</item>
<item name="android:windowCloseOnTouchOutside">false</item>
</style>
<activity
android:name="xActivity"
android:theme="@style/AppTheme.Transparent" />
I just did two things, and it made my activity transparent. They are below.
In the manifest file I just added the below code in the activity tag.
android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"
And I just set the background of the main layout for that activity as "#80000000". Like
android:background="#80000000"
It perfectly works for me.
Assign it the Translucent theme
android:theme="@android:style/Theme.Translucent.NoTitleBar"
There're two ways:
- Using Theme.NoDisplay
- Using Theme.Translucent.NoTitleBar
Using Theme.NoDisplay
will still work… but only on older Android devices. On Android 6.0 and higher, using Theme.NoDisplay without calling finish()
in onCreate() (or, technically, before onResume())
will crash your app. This is why the recommendation is to use Theme.Translucent.NoTitleBar
, which does not suffer from this limitation.”
Note 1:In Drawable folder create test.xml and copy the following code
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke android:width="2dp" />
<gradient
android:angle="90"
android:endColor="#29000000"
android:startColor="#29000000" />
<corners
android:bottomLeftRadius="7dp"
android:bottomRightRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp" />
</shape>
// Note: Corners and shape is as per your requirement.
// Note 2:Create xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/test"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.09"
android:gravity="center"
android:background="@drawable/transperent_shape"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
If you are using AppCompatActivity
then add this in styles.xml
<style name="TransparentCompat" parent="@style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowNoTitle">true</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>
In manifest
file you can add this theme to activity tag like this
android:theme="@style/TransparentCompat"
for more details read this article
Just add the following line to the activity tag in your manifest file that needs to look transparent.
android:theme="@android:style/Theme.Translucent"
All those answers might be confusing, there is a difference between Transparent activity and None UI activity.
Using this:
android:theme="@android:style/Theme.Translucent.NoTitleBar"
Will make the activity transparent but will block the UI.
If you want a None UI activity than use this:
android:theme="@android:style/Theme.NoDisplay"
You can remove setContentView(R.layout.mLayout)
from your activity and set theme as android:theme="@style/AppTheme.Transparent"
. Check this link for more details.
참고URL : https://stackoverflow.com/questions/2176922/how-do-i-create-a-transparent-activity-on-android
'development' 카테고리의 다른 글
jQuery AJAX 제출 양식 (0) | 2020.09.28 |
---|---|
bash에서 ls를 사용하여 디렉토리 만 나열 : 검사 (0) | 2020.09.28 |
JavaScript 앞에 CSS를 포함하라는 권장 사항이 유효하지 않습니까? (0) | 2020.09.28 |
Java의 toString ()에서 StringBuilder 대 문자열 연결 (0) | 2020.09.28 |
$ scope. $ emit 및 $ scope. $ on 작업 (0) | 2020.09.28 |