2016-04-08 8 views
0

カスタムダイアログボックスを表示するコードを記述しました。コードに書かれているように、背景が透明になっていますが、ダイアログボックスは表示されません。ダイアログボックスが表示されない

protected void initiatedialog(View view) { 

    final Dialog dialog = new Dialog(PostsActivity.this); 
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    dialog.setContentView(R.layout.popup); 

    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT)); 
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); 
    lp.copyFrom(dialog.getWindow().getAttributes()); 
    lp.width = Math.round((width * 10)/15); 
    lp.height = Math.round(height/3); 
    dialog.getWindow().setAttributes(lp); 
    final EditText etPostContent = (EditText) dialog.findViewById(R.id.etPostContent); 
    Button bAddPost = (Button) dialog.findViewById(R.id.bAddPost); 


    bAddPost.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      //something 
     dialog.dismiss(); 
     } 
    }); 
    dialog.show(); 
} 

ここには、ダイアログボックスのカスタマイズ用のxmlファイルがあります。それは私の携帯電話に取り組んでいます(アンドロイド4.0.3) - すべての最初の、[OK]を

popup.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center" 
    android:background="@drawable/popup_background"> 
    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="3" 
     android:id="@+id/etButton" 
     android:hint="Write.."/> 
    <Button 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/bAdd" 
     android:layout_weight="7" 
     android:text="Button"/> 

+0

アラートダイアログボックスを試してください –

答えて

0



私はあなたの変数/ drawablesのいくつかを知らないので、少し変更されたコードを入れて違いを説明します。

1. Popup.xml:私は知りません

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center"> 
    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="3" 
     android:id="@+id/etPostContent" 
     android:hint="Write.."/> 
    <Button 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/bAddPost" 
     android:layout_weight="7" 
     android:text="Button"/> 

</LinearLayout> 

あなたandroid:background="@drawable/popup_background" - まず、この行を削除し、問題が残っているかどうかを確認してみてください。
また、Javaファイルの別の名前を指しているため、ビューのIDを変更しました。

2. MainActivity(インポートあり)。

import android.app.Dialog; 
import android.content.Context; 
import android.graphics.drawable.ColorDrawable; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.view.Window; 
import android.view.WindowManager; 
import android.widget.Button; 
import android.widget.EditText; 

public class MainActivity extends AppCompatActivity { 

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

     initiatedialog(null); 
    } 


    protected void initiatedialog(View view) { 

     final Dialog dialog = new Dialog(MainActivity.this); 
     dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     dialog.setContentView(R.layout.popup); 

     dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT)); 
     WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); 
     lp.copyFrom(dialog.getWindow().getAttributes()); 

     int width = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth(); 
     int height = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getHeight(); 
     lp.width = Math.round((width * 10)/15); 
     lp.height = Math.round(height/3); 
     dialog.getWindow().setAttributes(lp); 
     final EditText etPostContent = (EditText) dialog.findViewById(R.id.etPostContent); 
     Button bAddPost = (Button) dialog.findViewById(R.id.bAddPost); 

     bAddPost.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       //something 
       dialog.dismiss(); 
      } 
     }); 
     dialog.show(); 
    } 
} 

ビットのコメント - 私は知らない、あなたの幅と高さが何であるか - 私は、ウィンドウの幅と高さを取りました。
また、輸入品をご確認ください。
enter image description here

PS:

結果を参照してください。ちょっとしたアドバイス - あなたはlayout_weight xml属性が働きたいと思っています - layout_width = "0dp"を設定してください。変更された結果を試してみてください。参照:
enter image description here

上記の変更を加えて、何が起こっているかを確認してください。ありがとう。

+0

何も助けになりませんでした。ダイアログボックスは表示されません。 – TeeKay

+0

申し訳ありません!出来た。私は行を追加することを忘れていた。助けてくれてありがとう。 – TeeKay

関連する問題