2017-02-06 23 views
0

カスタムアラートダイアログの幅が画面サイズに合わず、何も問題がないと思われる多くの方法を試しました(アラートダイアログの幅が画面サイズに完全に一致する必要があります)あなたのアイデアを共有してください。アラートダイアログのカスタムレイアウトの幅が画面に合わない

import android.app.Activity; 
import android.app.Dialog; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.Window; 
import android.widget.Button; 
import android.widget.RelativeLayout; 

public class Sample extends Activity { 

final Context context = this; 
Button btn; 

public void onCreate(Bundle savedInstanceState) { 

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

    btn = (Button) findViewById(R.id.buttonShowCustomDialog); 
    btn.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

      // custom dialog 
      final Dialog dialog = new Dialog(context); 
      dialog.setContentView(R.layout.research); 
      dialog.setTitle("Title..."); 
      dialog.show(); 

     } 
    }); 
} 
} 

//simple.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:orientation="vertical" > 

<Button 
    android:id="@+id/buttonShowCustomDialog" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Show Custom Dialog" /> 

</LinearLayout> 

//research.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="#00555555" 
    > 

<RelativeLayout 
    android:id="@+id/pakka" 
    android:layout_width="fill_parent" 
    android:layout_height="200dp" 
    android:layout_centerInParent="true" 
    android:background="#c8c8c8"> 

    <TextView 
     android:id="@+id/hello" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:padding="22dp" 
     android:text="Hi"/> 

    <TextView 
     android:id="@+id/nik" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/hello" 
     android:layout_centerHorizontal="true" 
     android:padding="5dp" 
     android:text="Hi"/> 

    <Button 
     android:layout_width="360dp" 
     android:layout_height="60dp" 
     android:layout_below="@+id/peetnik" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="20dp" 
     android:text="Login to see"/> 
    </RelativeLayout> 


</RelativeLayout> 

答えて

-1

この試してください:あなたのres /値/スタイルで

を:

<style name="dialog_style" parent="android:style/Theme.Dialog"> 
    <item name="android:windowNoTitle">true</item> 
    <item name="android:background">@android:color/transparent</item> 
    <item name="android:windowBackground">@android:color/transparent</item> 
</style> 

今すぐあなたの活動に表示するには、次のコードを使用しますダイアログ:

 final Dialog dialog = new Dialog(Sample.this,R.style.dialog_style); 
    dialog.setContentView(R.layout.research); 
    dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PA‌​RENT, ViewGroup.LayoutParams.MATCH_PARENT); 
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 
    dialog.setCancelable(true); 
    dialog.setCanceledOnTouchOutside(true); 
    dialog.show(); 
ここ

ViewGroup.LayoutParams.MATCH_PARENTは画面幅に

+0

幅が画面に完全に適合しなかったWindowManager.LayoutParams()を使う:( – HsRaja

+0

は、これを追加します。ダイアログ 'は、GetWindow()の背景でsetBackgroundDrawable(新ColorDrawable(Color.TRANSPARENT )); ' – rafsanahmad007

+0

は完全にはまっていません。スクリーンショットを表示できますか? – rafsanahmad007

-1

使用dialog.getWindow()を充填するsetLayoutの(SCREEN_WIDTH、SCREEN_HEIGHT)。

0

単に

final Dialog dialog = new Dialog(getActivity()); 
      dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
      dialog.setContentView(R.layout.research); 
      WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); 
      lp.copyFrom(dialog.getWindow().getAttributes()); 
      lp.width = WindowManager.LayoutParams.MATCH_PARENT; 
      lp.height = WindowManager.LayoutParams.MATCH_PARENT; 

      dialog.show(); 
      dialog.getWindow().setAttributes(lp); 
+1

ここでは幅がフルスクリーンにフィットし、また、その白の背景には、このと一緒に来ていない:( – HsRaja

+0

背景色のためにあなたがdialog.getWindowを(使用することができます) \t \t \t \t .setBackgroundDrawable(新しいColorDrawable(Color.WHITE))であった。白何でもかまいません。 –

関連する問題