2011-06-07 152 views
98

我有一個奇怪的行爲,我無法確定的來源。Android對話框:刪除標題欄

我有我的應用程序與經典

requestWindowFeature(Window.FEATURE_NO_TITLE); 

刪除標題/狀態欄。

然後創建一個對話框,允許用戶輸入信息(姓名等)

帶物理鍵盤,沒有問題,但是當我使用虛擬鍵盤我有一個奇怪的現象:

各一次,我打在虛擬鍵盤的標題/狀態欄會重新出現推動全鍵盤佈局圍繞然後消失再次(就像當我啓動應用程序的動畫)

這裏的關鍵是一些代碼:

 dialog = new Dialog(context); 
     dialog.setContentView(R.layout.logindialog); 
     dialog.setTitle("Login:"); 

     WindowManager.LayoutParams a = dialog.getWindow().getAttributes(); 

//  dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 

     a.dimAmount = 0; 
     dialog.getWindow().setAttributes(a); 

     dialog.setCancelable(true); 
     dialog.getWindow().setLayout(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT); 

然後

dialog.show(); 

我試圖

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 

,但它崩潰我的應用程序。

這裏是XML

<TextView android:id="@+id/LoginText" 
     android:gravity="fill" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Login:"> 
    </TextView>   
    <EditText android:id="@+id/LoginEdit" 
     android:layout_height="wrap_content" 
     android:singleLine="true" 
     android:text="jason" 
     android:layout_width="200sp"/> 
    <TextView android:id="@+id/PasswordText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Password:"> 
    </TextView>   
    <EditText android:id="@+id/PasswordEdit" 
     android:layout_height="wrap_content" 
     android:singleLine="true" 
     android:text="welcome" 
     android:layout_width="200sp" 
     android:password="true"/> 
<LinearLayout 
    android:id="@+id/test2" 
    android:gravity="center_horizontal" 
    android:orientation="horizontal" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
<Button android:id="@+id/LoginButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:text="Login" /> 
<Button android:id="@+id/CreateButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:text="Create" /> 
<Button android:id="@+id/CancelLogin" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:text="Cancel" /> 
</LinearLayout>/> 

+0

當您使用dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)時發佈您的logcat; 同時使用上面的代碼行不要使用dialog.setTitle(「Login:」); – ingsaurabh 2011-06-07 10:12:56

+0

感謝您的建議,但它不是問題。發生什麼事情是當我用鍵盤輸入某些東西時,狀態欄不斷出現並隨着每次擊鍵而消失。 – 2011-06-07 11:01:16

回答

405

使用,

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); //before  
dialog.setContentView(R.layout.logindialog); 
+0

感謝您的建議,但它不是問題。發生什麼事情是當我用鍵盤輸入某些東西時,狀態欄不斷出現並隨着每次擊鍵而消失。 – 2011-06-07 11:01:10

+9

在某些設備上,這會導致對話框佈局失去寬度,但保持高度。我發現在.show()之前添加了這個功能,儘管它們已經在xml佈局中設置了。 dialog.getWindow()。setLayout(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.WRAP_CONTENT); – a54studio 2015-02-07 05:23:07

+0

在DialogFragment的繼承者的情況下,我們需要將'getDialog()。requestWindowFeature(Window.FEATURE_NO_TITLE);'onCreateView'內部。 – 2017-05-15 15:05:00

2

您還可以在Android清單文件中定義的主題爲不顯示山雀樂吧..

你只要在活動,其中u不用想顯示標題欄

例定義主題android:theme="@android:style/Theme.Light.NoTitleBar": -

<uses-sdk android:minSdkVersion="4"android:targetSdkVersion="4" /> 
<application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <activity android:name=".splash" 
       android:label="@string/app_name" android:screenOrientation="portrait" 
       android:theme="@android:style/Theme.Light.NoTitleBar"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

<activity android:name="main" android:screenOrientation="portrait" android:theme="@android:style/Theme.Light.NoTitleBar"></activity> 

+0

我已經有一個沒有狀態欄的活動,但是當我在虛擬鍵盤上打字時,狀態欄出現並消失(推動所有佈局),就像開始活動時狀態欄消失一樣。只有這次它每次觸摸鍵盤時 – 2011-06-07 12:19:06

22

創建新風格。的setContentView之前,下面的代碼

<activity android:name=".youractivity" android:theme="@style/myDialog"></activity> 
1

使用:XML

<style name="myDialog" parent="android:style/Theme.Dialog"> 
    <item name="android:windowNoTitle">true</item> 
</style> 

然後添加到您的清單 -

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
dialog.setContentView(R.layout.custom_dialog); 

注: -上面的代碼必須使用上面dialog.setContentView(R.layout.custom_dialog);

在XML使用主題

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

也styles.xml:

<style name="hidetitle" parent="android:style/Theme.Dialog"> 
    <item name="android:windowNoTitle">true</item> 
</style> 

然後:

Dialog dialog_hidetitle_example = new Dialog(context, R.style.hidetitle); 
4

創建XML這是在對話框 這裏顯示它是activity_no_title_dialog

final Dialog dialog1 = new Dialog(context); 
dialog1.requestWindowFeature(Window.FEATURE_NO_TITLE); 
dialog1.setContentView(R.layout.activity_no_title_dialog); 
dialog1.show(); 
1

我是一個Android新手,遇到了這個問題試圖擺脫標題欄。

我正在使用一個活動並將其作爲對話框顯示。我正在探討主題,並發現了一些有用的默認主題。

下面是從我AndroidManifest.xml當標題欄是顯示出我使用的代碼:

<activity 
    android:name=".AlertDialog" 
    android:theme="@android:style/Theme.Holo.Dialog" 

    > 

</activity> 

這裏是一個讓我我想要的結果的變化:

<activity 
    android:name=".AlertDialog" 
    android:theme="@android:style/Theme.Holo.Dialog.NoActionBar" 

    > 

</activity> 

正如我所說的,我對於Android來說還是個新手,所以這可能會產生不良的副作用,但它似乎給了我想要的結果,這只是從對話框中刪除標題欄。

0

這對我有效。

Dailog dialog = new Dialog(MyActivity.this, R.style.dialogstyle); 

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="dialogstyle" parent="android:style/Theme.Dialog"> 
     <item name="android:windowBackground">@null</item> 
     <item name="android:windowNoTitle">false</item> 
    </style> 
</resources> 
0

您可以試試這個簡單的android dialog popup library。使用您的活動非常簡單。

當點擊提交按鈕嘗試下面的代碼,包括上述的lib在你的代碼後

Pop.on(this) 
    .with() 
    .title(R.string.title) //ignore if not needed 
    .icon(R.drawable.icon) //ignore if not needed 
    .cancelable(false) //ignore if not needed 
    .layout(R.layout.custom_pop) 
    .when(new Pop.Yah() { 
     @Override 
     public void clicked(DialogInterface dialog, View view) { 
      Toast.makeText(getBaseContext(), "Yah button clicked", Toast.LENGTH_LONG).show(); 
     } 
    }).show(); 

在gradle這個添加一條線,你很好,如果你使用的是自定義的去

dependencies { 
    compile 'com.vistrav:pop:2.0' 
} 
2

查看然後在添加內容視圖之前記得請求窗口功能

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
dialog.setContentView(view); 
dialog.show(); 
7

以上所有的答案不是爲我工作的AppCompatDialog

如果您正在使用AppCompatDialog試試這個

重要提示:調用的setContentView之前設置此。

dialog.supportRequestWindowFeature(Window.FEATURE_NO_TITLE);

+0

謝謝,那是我的問題,讓我發瘋。 – gregko 2018-02-25 23:53:10