programing

안드로이드에서 투명한 활동을 만들려면 어떻게 해야 합니까?

mailnote 2023. 6. 30. 22:31
반응형

안드로이드에서 투명한 활동을 만들려면 어떻게 해야 합니까?

다른 활동 위에 투명한 활동을 작성합니다.

어떻게 하면 이를 달성할 수 있을까요?

다스타을추다니합에 다음 하세요.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>

(ppm)@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" />

저의 경우 몇 가지 조건에 따라 자바로 런타임에 테마를 설정해야 합니다.그래서 저는 하나의 주제를 스타일로 만들었습니다(다른 답변과 유사함).

<?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시간 동안 꼼짝도 하지 않았습니다.

저도 안드로이드 신규 개발자이기 때문에 조금 더 추가하고 싶었습니다.받아들여진 답변은 훌륭하지만, 문제가 생겼습니다.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" 태그를 찾는 것을 의미한다는 것을 이해하지 못했습니다.저는 다른 사람들을 돕기 위해 이것도 언급해야 한다고 생각했습니다.

.3.을 2.3.3에 추가하여 2을 했습니다.android:theme="@android:style/Theme.Translucent"매니페스트의 활동 태그에 있습니다.

하위 버전에 대해서는 잘 모르겠습니다...

2021년 사실

그냥 추가

<item name="android:windowBackground">@android:color/transparent</item>

모든 작업이 완료됩니다.

windowIsFloating틀렸습니다. 그러면 INSET 부동 창이 됩니다.

windowContentOverlay그림자에만 관련이 있습니다.

windowIsTranslucent틀렸습니다. 뒤에 있는 활동을 볼 수 있도록 만들지 않습니다. windowIsTranspective는 애니메이션 전환 시에만 관련이 있습니다.

backgroundDimEnabled아의활희만지다른완는버전발다니생합그가히서장치에래 (사용하지 도 하지 ; 으로 동작은 (경우에 따라 windowIsFloating을 사용하지 않는 한 아무 것도 작동하지 않습니다. 일반적으로 동작은 완전히 버그가 있거나 불확실합니다.)

colorBackgroundCacheHint매우 오래된 장치를 제외하고는 관련이 없으며, 기본값은 null입니다.

onCreate 함수의 setContentView 아래에 다음 행을 추가합니다.

getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

활동 배경 이미지를 투명하게 유지합니다.또는 XML 파일에 테마를 추가합니다.

<activity android:name=".usual.activity.Declaration" android:theme="@android:style/Theme.Translucent.NoTitleBar" />

쉬운 를 내가찾은가쉬장방 Android Manifest활동테마것으로 입니다.android:theme="@android:style/Theme.Holo.Dialog".

에서 OnCreate 를 호출합니다.getWindow().setBackgroundDrawable(new ColorDrawable(0));.

위의 답변 외에도:

활동 시 안드로이드 오레오 관련 충돌을 방지

<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" />

대화 상자 활동의 경우 다음을 사용합니다.

getWindow().getDecorView().setBackgroundResource(android.R.color.transparent);

그러나 활동의 기본 보기를 보이지 않도록 설정해야 합니다.그렇지 않으면 배경은 보이지 않지만 배경의 모든 보기는 표시됩니다.

사용 중인 경우 다음에 추가합니다.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"

자세한 내용은 이 기사를 참조하십시오.

저는 단지 두 가지 일을 했을 뿐이고, 그것은 제 활동을 투명하게 만들었습니다.그들은 아래에 있습니다.

  1. 매니페스트 파일에 아래 코드를 액티비티 태그에 추가했습니다.

    android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"
    
  2. 그리고 그 활동의 메인 레이아웃 배경을 "#80000000"으로 설정했습니다.맘에 들다

    android:background="#80000000"
    

저한테 딱 맞습니다.

반투명 테마 지정

android:theme="@android:style/Theme.Translucent.NoTitleBar"

두 가지 방법이 있습니다.

  1. 테마 사용.표시 안 함
  2. 테마 사용.반투명.제목 표시줄 없음

용사를 합니다.Theme.NoDisplay오래된 Android 장치에서만 작동합니다.Android 6.0 이상 버전에서는 테마를 사용합니다.하지 No 가 표시되지 않습니다.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>

매니페스트 파일의 활동 태그에 투명하게 보여야 하는 다음 행을 추가하기만 하면 됩니다.

android:theme="@android:style/Theme.Translucent"

이러한 모든 답변은 혼란스러울 수 있습니다. 투명 활동과 없음 UI 활동 사이에는 차이가 있습니다.

사용 방법:

android:theme="@android:style/Theme.Translucent.NoTitleBar"

활동을 투명하게 만들지만 UI를 차단합니다.

UI 없음 작업을 수행하려면 다음을 사용합니다.

android:theme="@android:style/Theme.NoDisplay"

있니다를 제거할 수 .setContentView(R.layout.mLayout)당신의 활동으로부터 그리고 테마를 설정합니다.android:theme="@style/AppTheme.Transparent"자세한 내용은 이 링크를 참조하십시오.

이것을 style.xml에 넣으십시오.

<item name="android:windowBackground">@android:color/transparent</item>

매니페스트에 oR 추가

<activity android:name=".usual.activity.Declaration" 
 android:theme="@android:style/Theme.Translucent.NoTitleBar" />

다른 해결책들은 저에게 효과가 있었지만, 저는 일부에 영향을 미칠 수 있는 문제를 발견했습니다.이렇게 활동을 시작할 때:

Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);

이런 원라이너 대신에

startActivity(new Intent(this, MainActivity.class));

저는 그냥 검은 배경을 가지고 있어서 괴롭습니다.저는 이것의 원인이 무엇인지 파악하지 못했습니다. 아마도 기기 관련인 것 같습니다(Xiaomi에서 테스트됨).그래서 가치관을 전달하는 것 또한 이렇게 되어야 합니다.

startActivity(new Intent(this, MainActivity.class)
                            .putExtra("SOME_VALUE", value)
                            .putExtra("ANOTHER_VALUE", value2));

편집: 스타일의 부모를 테마로 변경합니다.AppCompat.Light.NoActionBar에서 이 문제를 해결한 것 같습니다.

Gnobal의 위 솔루션과 함께 특정 활동의 레이아웃 파일에서 알파를 0으로 설정해야 했습니다. 특정 전화기(Android 10에서 실행되는 Redmi Narzo 20 pro)에서 화면의 대화 부분이 투명해야 하는 화면과 함께 표시되었기 때문입니다.어떤 이유에서인지 창 IsFloating이 이 문제를 일으켰지만 창을 제거할 때 원하는 출력을 얻지 못했습니다.

단계:

  1. res > values > style.xml 아래에 있는 style.xml에 다음을 추가합니다.

     <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>
       <item name="android:colorBackgroundCacheHint">@null</item>
     </style>
    

  2. AndroidManifest.xml에서 위의 스타일로 활동 테마 설정

    <activity
          android:name=".activityName"
          android:theme="@style/Theme.Transparent"/>
    

  3. 위의 스타일을 적용한 활동의 레이아웃 파일을 열고 알파 값을 0(android:alpha="0"부모 레이아웃 요소의 경우.

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 
        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"
        android:alpha="0">
    
      <WebView        
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:alpha="0"/>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

참고: You'll have to extend you activity using 활동() class and AppCompativity가 아님 for using the above solution.

언급URL : https://stackoverflow.com/questions/2176922/how-do-i-create-a-transparent-activity-on-android

반응형