your programing

기존 사용자 지정 테마를 사용하여 XML에서 활동의 제목 표시 줄을 숨기는 방법

lovepro 2020. 9. 28. 09:50
반응형

기존 사용자 지정 테마를 사용하여 XML에서 활동의 제목 표시 줄을 숨기는 방법


일부 활동의 제목 표시 줄을 숨기고 싶습니다. 문제는 모든 활동에 스타일을 적용했기 때문에 단순히 테마를로 설정할 수 없다는 것 @android:style/Theme.NoTitleBar입니다.

내 스타일의 부모로 NoTitleBar 테마를 사용하면 내 모든 활동에서 제목 표시 줄이 제거됩니다.

제목 없음 스타일 항목을 어딘가에 설정할 수 있습니까?


당신의 onCreate()방법으로 이것을하십시오 .

//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);

//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

//set content view AFTER ABOVE sequence (to avoid crash)
this.setContentView(R.layout.your_layout_name_here); 

thisActivity.


다음을 수정할 수 있습니다 AndroidManifest.xml.

<activity android:name=".MainActivity"
          android:label="@string/app_name"
          android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">

또는 android:theme="@android:style/Theme.Black.NoTitleBar"전체 화면 활동이 필요하지 않은 경우 사용 하십시오.

참고 : 이전에 '기본'보기를 사용한 적이 있다면 상위 클래스도에서 AppCompatActivity변경해야 합니다 Activity.


이제 다음을 수행했습니다.

일반적인 스타일에서 모든 것을 상속받은 스타일을 선언 한 다음 titleBar를 비활성화했습니다.

<style name="generalnotitle" parent="general">
    <item name="android:windowNoTitle">true</item>
</style>

이제이 스타일을 애플리케이션 전체 스타일을 덮어 쓰고 다른 모든 스타일 정보를 상속하는 제목 표시 줄을 숨기려는 모든 활동에 설정할 수 있으므로 스타일 코드가 중복되지 않습니다.

특정 활동에 스타일을 적용하려면 AndroidManifest.xml다음 속성을 열고 activity태그에 추가하십시오 .

<activity
    android:theme="@style/generalnotitle">

this.requestWindowFeature(Window.FEATURE_NO_TITLE);제목 표시 줄이 잠시 나타났다가 사라지기 때문에 마음에 들지 않습니다 .

나는 또한 android:theme="@android:style/Theme.NoTitleBar"새로운 장치의 사용자가 익숙해 진 3.0+ Holo 변경 사항을 모두 잃었 기 때문에 좋아하지 않습니다 . 그래서이 솔루션을 발견했습니다.

당신에 고해상도 / 값 라는 파일을 만들 폴더 styles.xml (이미 존재하지 않는 경우). 해당 파일에 다음 코드를 넣으십시오.

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="Theme.Default" parent="@android:style/Theme"></style>
    <style name="Theme.NoTitle" parent="@android:style/Theme.NoTitleBar"></style>
    <style name="Theme.FullScreen" parent="@android:style/Theme.NoTitleBar.Fullscreen"></style>
</resources>

다음 으로 다른 styles.xml 파일을 사용하여 res / values-v11만듭니다 (다시 한번이 파일이 이미있을 수 있습니다). 해당 파일에 다음 코드를 넣으십시오.

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="Theme.Default" parent="@android:style/Theme.Holo"></style>
    <style name="Theme.NoTitle" parent="@android:style/Theme.Holo.NoActionBar"></style>
    <style name="Theme.FullScreen" parent="@android:style/Theme.Holo.NoActionBar.Fullscreen"></style>
</resources>

4.0 이상을 대상으로하는 경우 또 다른 styles.xml 파일이 있는 res / values-v14 폴더를 만듭니다 (예, 이미있을 수 있음). 해당 파일에 다음 코드를 넣으십시오.

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="Theme.Default" parent="@android:style/Theme.Holo.Light"></style>
    <style name="Theme.NoTitle" parent="@android:style/Theme.Holo.Light.NoActionBar"></style>
    <style name="Theme.FullScreen" parent="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen"></style>
</resources>

마지막으로 이러한 파일이 모두 생성되면 AndroidManifiest.xml 파일을 열고 코드를 추가 할 수 있습니다.

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

제목을 원하지 않는 활동의 활동 태그 또는 전체 애플리케이션에 적용하려는 경우 애플리케이션 태그에 추가하십시오.

이제 사용자는 원하는 화면 레이아웃으로 장치 버전과 관련된 테마를 받게됩니다.

PS 값을로 변경하면 android:theme="@style/Theme.FullScreen"동일한 효과가 있지만 알림 표시 줄도 제거됩니다.


제목 표시 줄은 개발자 Android 페이지에 언급 된 두 가지 방법으로 제거 할 수 있습니다.

에서 manifest.xml파일 :

  1. application앱의 모든 활동에 대해 제거 하려면 다음을 추가하십시오 .

    <application android:theme="@android:style/Theme.Black.NoTitleBar">
    
  2. 또는 특정 활동의 경우 :

    <activity android:theme="@android:style/Theme.Black.NoTitleBar">
    

정답은 아마도 ActionbarActivity를 확장하지 않고 Activity를 확장하는 것입니다.


여전히 작업 표시 줄 활동을 사용하는 경우 이것이 작동하는 것 같습니다.

@Override
protected void onCreate(Bundle savedInstanceState) {          
    super.onCreate(savedInstanceState);
    getSupportActionBar().hide(); //<< this
    setContentView(R.layout.activity_main);
}

이것도 작동하는 것 같습니다.

styles.xml:

   <style name="AppBaseTheme" parent="Theme.AppCompat.Light" >
          <item name="android:windowNoTitle">true</item>   <!-- //this -->     
   </style>

나는 Scott Biggs가 쓴 것처럼 할 수 있습니다. 이런 종류의 작품. 그때 테마가 없다는 것을 제외하고. 설정 메뉴의 배경이 투명하다는 의미입니다.

그냥 변경

public class MainActivity extends ActionBarActivity {

활동 또는 FragmentActivity

public class MainActivity extends Activity  {

그러나 머티리얼 디자인을 사용하여 충분히보기 좋게 만들고 액션 바를 제거하지 않을 수 있습니다 : https://gist.github.com/shimondoodkin/86e56b3351b704a05e53

  1. 응용 프로그램 아이콘 설정
  2. 디자인에 맞게 액션 바의 색상을 설정합니다.
  3. 아이콘을 설정 메뉴로 설정
  4. 더 많은 아이콘 추가 (상단 버튼)

머티리얼 디자인 호환성 액션 바 스타일링의 예입니다.


를 사용 this.requestWindowFeature(Window.FEATURE_NO_TITLE)하는 경우 활동이 시작될 때 시작 애니메이션 동안 잠시 동안 제목 표시 줄을 볼 수 있습니다 onCreate. @android:style/Theme.Black.NoTitleBar아래와 같이 사용하면 애니메이션 실행시 제목 표시 줄이 표시되지 않습니다.

<activity 
    android:name=".MainActivity" 
    android:label="My App"
    android:theme="@android:style/Theme.Black.NoTitleBar"
    android:screenOrientation="portrait">

위의 예는 기존 테마가있는 경우 기존 응용 프로그램 테마를 분명히 덮어 씁니다 <item name="android:windowNoTitle">true</item>.


나를 위해 작동하는 것 :

1- styles.xml에서 :

 <style name="generalnotitle" parent="Theme.AppCompat.Light" >
          <item name="android:windowNoTitle">true</item>   <!-- //this -->     
   </style>

2- MainActivity에서

@Override
protected void onCreate(Bundle savedInstanceState) {          
    super.onCreate(savedInstanceState);
    getSupportActionBar().hide(); //<< this
    setContentView(R.layout.activity_main);
}
  1. 매니페스트에서 스타일을 상속합니다.

    <activity android:name=".MainActivity" android:theme="@style/generalnotitle">
    

내가 높은 upvoted 답변을 모두 사용하려고 할 때 내 앱이 항상 충돌했습니다. ( "@android : style"과 관련이 있다고 생각합니까?)

나에게 가장 좋은 해결책은 다음을 사용하는 것입니다.

android:theme="@style/Theme.AppCompat.NoActionBar"

더 이상 헤더 / 제목 표시 줄이 없습니다. 전체 앱 또는 특정 활동에서 원하는지 여부에 따라 <application>...</application>또는 <activity>...</activity>배치하십시오 .


아래와 같이 테마를 생성합니다.

 <!-- Variation on the Light theme that turns off the title -->
<style name="MyTheme" parent="android:style/Theme.Black">
    <item name="android:windowNoTitle">true</item>
</style>

더하다

<item name="android:windowNoTitle">true</item>

AppTheme 내부 (styles.xml)


getActionBar().hide();주요 활동 onCreate()방법에 사용 하십시오 .


AppTheme정의를 바꿀 경우 Style.xml에서 스타일 을 변경하기 만하면됩니다.DarkActionBar

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

...에 NoActionBar

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

AndroidManifast.xml에 정의 된 AppTheme


의 경우 AppCompat다음 솔루션이 저에게 효과적이었습니다.

작업 표시 줄이없는 새 테마 스타일을 추가 styles.xml하고 parent="Theme.AppCompat.NoActionBar".

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">

    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimary</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowBackground">@color/colorPrimary</item>

</style>


이제 스플래시 화면 활동에 동일한 테마 스타일을 구현하십시오. androidManifest.xml

<activity
        android:name=".ActivityName"
        android:theme="@style/SplashTheme"> // apply splash them here 

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
</activity>

결과는 다음과 같습니다.

여기에 이미지 설명 입력


이 코드를 Java 파일에서 사용할 수 있습니다.

보기를 설정하거나로드하기 전에이 줄을 추가하십시오.

requestWindowFeature(Window.FEATURE_NO_TITLE); 
setContentView(R.layout.activity_main);

당신에 onCreate방법, 다음 코드를 사용합니다 :

this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);

사용하는 테마에 대해 둘 다 추가하십시오.

    <item name="windowActionBar">false</item>
    <item name="android:windowNoTitle">true</item>

이 스타일을 style.xml 파일에 추가하십시오.

 <style name="AppTheme.NoActionBar">
     <item name="windowActionBar">false</item>
     <item name="windowNoTitle">true</item>
 </style>

그런 다음 제목 표시 줄을보고 싶지 않은 관련 활동의 androidManifest.xml에이 스타일 이름을 참조하십시오.

<activity android:name=".youractivityname"
     android:theme="@style/AppTheme.NoActionBar">
</activity>

또는 언제든지 제목 표시 줄을 숨기거나 표시하려면 :

private void toggleFullscreen(boolean fullscreen)
{
    WindowManager.LayoutParams attrs = getWindow().getAttributes();
    if (fullscreen)
    {
        attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
    }
    else
    {
        attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
    }
    getWindow().setAttributes(attrs);
}

지원 위젯 Toolbar v7을 사용하고 있습니다. 따라서 제목을 삭제하거나 숨기려면 이것을 작성해야합니다.

Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);

//Remove¡ing title bar
getSupportActionBar().setDisplayShowTitleEnabled(false);

제 경우에는 android studio 2.1을 사용하고 컴파일 SDK 버전이 6.0이면 manifest.xml 파일로 이동하여 다음 코드를 변경하십시오.

다음은 코드입니다.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lesterxu.testapp2">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.NoActionBar">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

다음은 캡처 촬영입니다 (하이라이트 코드 참조). 여기에 이미지 설명 입력


This Solved my problem

In manifest my activity:

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".SplashScreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

In style under "AppTheme" name:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme"
        parent="Theme.AppCompat.Light.NoActionBar">
        **<item name="windowActionBar">false</item>
        <item name="android:windowNoTitle">true</item>**
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

To Hide both Title and Action bar I did this:

In activity tag of Manifest:

android:theme="@style/AppTheme.NoActionBar"

In Activity.java before setContentView:

this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

i.e.,

//Remove title bar

this.requestWindowFeature(Window.FEATURE_NO_TITLE);

//Remove notification bar

this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

NOTE: You need to keep these lines before setContentView


First answer is amphibole. here is my explain: add:

this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

in oncreate() method.

before:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);

(not just before setContentView) if don't do this u will get forceclose. +1 this answer.


I found two reasons why this error might occur.

One. The Window flags are set already set inside super.onCreate(savedInstanceState); in which case you may want to use the following order of commands:

this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);      

super.onCreate(savedInstanceState);

Two. You have the Back/Up button inside the TitleBar, meaning that the current activity is a hierarchical child of another activity, in which case you might want to comment out or remove this line of code from inside the onCreate method.

getActionBar().setDisplayHomeAsUpEnabled(true);

This is how the complete code looks like. Note the import of android.view.Window.

package com.hoshan.tarik.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
    }
}

Add theme @style/Theme.AppCompat.Light.NoActionBar in your activity on AndroidManifest.xml like this

<activity
        android:name=".activities.MainActivity"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar">
    </activity>

If you do what users YaW and Doug Paul say, then you have to have in mind that window features must be set prior to calling setContentView. If not, you will get an exception.


add in manifiest file ,

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

add following line into ur java file,

  this.requestWindowFeature(Window.FEATURE_NO_TITLE);

Ahem, 제목 없음과 같은 XML의 개별 활동에 테마를 적용 할 수 있습니다 . 즉, 애플리케이션 태그에서 꺼내서 태그 선언을 열고 원하는 활동 태그에 넣습니다.

참고 URL : https://stackoverflow.com/questions/2591036/how-to-hide-the-title-bar-for-an-activity-in-xml-with-existing-custom-theme

반응형