답변:
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>
android:windowIsFloatingtrue 로 설정되어 있기 때문에 대화 상자처럼 작동 합니다. 정상적인 활동처럼 동작하려면이 속성을 제거하십시오 (이 경우 일치 android:style/Theme.Translucent.NoTitleBar)
AppCompatActivity. 그래서 parent="android:Theme"내 앱이 충돌했습니다. 방금 그것을 제거하고 매력처럼 작동했습니다. 감사!
다음과 같이 진행됩니다.
<activity android:name=".usual.activity.Declaration" android:theme="@android:style/Theme.Translucent.NoTitleBar" />
50% black should be #7f000000. 각 성분 (A, R, G, B)은의 값을 가질 수 있습니다 0-255. 50% of 255 = 127. 127 in Hex = 7F투명도 (불투명도)를 계산하는 방법
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>
Theme.Appcompat.Light.NoActionBar대신 상속받을 수 있습니다 .
background선호하는 반투명 한 색 을 제거 하고 넣기 를 원한다고 생각합니다 .windowBackground
나는 새로운 안드로이드 개발자이기도하므로 이것에 약간 추가하고 싶었습니다. 대답은 훌륭하지만 문제가 생겼습니다. 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"태그를 찾는다는 것을 이해하지 못했습니다. 다른 사람을 돕기 위해 이것을 언급해야한다고 생각했습니다.
android:theme="@android:style/Theme.Translucent"매니페스트에 활동 태그를 추가하여 2.3.3에 달성했습니다 .
나는 낮은 버전에 대해 모른다 ...
AppCompatActivity경우 사용하지 마십시오 .
내 경우에는 일부 조건을 기반으로 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>
그런 다음 Java에서 내 활동에 적용했습니다.
@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);
}
여기서 중요한 점 하나를 기억 하십시오. setTheme()전에 함수를 호출해야합니다 super.onCreate(savedInstanceState);. 나는이 시점을 놓치고 2 시간 동안 붙어서 왜 테마가 런타임에 반영되지 않는지 생각했습니다.
에서 에서 onCreate 기능, 아래의 된 setContentView 이 줄을 추가합니다 :
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
위의 답변 외에도 :
활동에서 안드로이드 오레오 관련 충돌을 피하기 위해
<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" />
방금 두 가지 일을 했으므로 활동이 투명 해졌습니다. 아래에 있습니다.
매니페스트 파일에서 방금 활동 태그 에 아래 코드를 추가했습니다 .
android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"그 활동에 대한 기본 레이아웃의 배경을 " # 80000000 "으로 설정했습니다. 처럼
android:background="#80000000"그것은 나를 위해 완벽하게 작동합니다.
당신이 사용하는 AppCompatActivity경우에 이것을 추가하십시오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>
에서 manifest파일이 같은 활동 태그에이 테마를 추가 할 수 있습니다
android:theme="@style/TransparentCompat"
자세한 내용은이 기사를 읽으십시오
사용 Theme.NoDisplay은 계속 작동하지만 이전 Android 기기에서만 작동합니다. 안드로이드 6.0에 높은 호출하지 않고 Theme.NoDisplay를 사용 finish()에서 onCreate() (or, technically, before onResume())됩니다 충돌 하여 응용 프로그램을. 이것이 권장 사항 을 사용 하는 이유 Theme.Translucent.NoTitleBar인데,이 제한 이 없습니다 .”
참고 1 : Drawable 폴더에 test.xml을 만들고 다음 코드를 복사하십시오.
<?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>
// 참고 : 모서리와 모양은 요구 사항에 따라 다릅니다.
// 참고 2 : 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>
<item name="android:windowBackground">@android:color/transparent</item>