2014-06-17 6 views
7

मैं एक कस्टम संवाद कर रहा हूं जिसमें मैं ऊंचाई और चौड़ाई सभी स्क्रीन आकारों के लिए डिफ़ॉल्ट फिट होना चाहता हूं।कस्टम डायलॉग ऊंचाई और चौड़ाई सेट करें

लेकिन ऐसा नहीं हो रहा है। हालांकि संवाद बहुत छोटा दिखाई दे रहा है। इस संवाद

<?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="wrap_content" 
android:background="#D24379" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/txt_dia" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:layout_margin="10dp" 
    android:textColor="@android:color/white" 
    android:textSize="15dp" 
    android:textStyle="bold" > 
</TextView> 

<LinearLayout 
    android:id="@+id/layButton" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="5dp" 
    android:orientation="horizontal" > 

    <Button 
     android:id="@+id/yesButton" 
     android:layout_width="0.0dip" 
     android:layout_height="wrap_content" 
     android:layout_weight="1.0" 
     android:background="#ffffff" 
     android:text="@string/yes" /> 

    <Button 
     android:id="@+id/noButton" 
     android:layout_marginLeft="5dp" 
     android:layout_width="0.0dip" 
     android:background="#ffffff" 
     android:layout_height="wrap_content" 
     android:layout_weight="1.0" 
     android:text="@string/no" /> 
</LinearLayout> 

के लिए एक्सएमएल है यह मेरा संवाद वर्ग

public class CustomDialogClass extends Dialog implements 
      android.view.View.OnClickListener { 

     public Activity c; 
     public Dialog d; 
     public Button yes, no; 
     public TextView content; 

     public CustomDialogClass(Activity a) { 
      super(a); 
      this.c = a; 
     } 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      requestWindowFeature(Window.FEATURE_NO_TITLE); 
      setContentView(R.layout.custom_dialog); 
      yes = (Button) findViewById(R.id.yesButton); 
      no = (Button) findViewById(R.id.noButton); 
      content=(TextView)findViewById(R.id.txt_dia); 
      yes.setOnClickListener(this); 
      no.setOnClickListener(this); 

     } 

     @Override 
     public void onClick(View v) { 
      switch (v.getId()) { 
      case R.id.yesButton: 

       break; 
      case R.id.noButton: 
       dismiss(); 
       break; 
      default: 
       break; 
      } 
      dismiss(); 
     } 
     } 

मैं कैसे एक डिफ़ॉल्ट आकार ऐसा लगता है जैसे कि पर सही प्रकट करने के लिए ऊंचाई और चौड़ाई सेट है सभी स्क्रीन आकार।

मुझे एक बहुत छोटा संवाद मिल रहा है।

उत्तर

1

मैं अपने रखना डाल कोशिश बदल गया है अब

<?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="150dp" 
     android:background="#D24379" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/txt_dia" 
      android:layout_width="match_parent" 
      android:layout_height="30dp" 
      android:layout_marginTop="10dp" 
      android:layout_weight="0.20" 
      android:text="Are you good?" 
      android:textColor="@android:color/white" 
      android:textSize="15dp" 
      android:textStyle="bold" /> 

     <LinearLayout 
      android:id="@+id/layButton" 
      android:layout_width="match_parent" 
      android:layout_height="80dp" 
      android:layout_margin="5dp" 
      android:gravity="center" 
      android:orientation="horizontal" > 

      <Button 
       android:id="@+id/yesButton" 
       android:layout_width="0.0dip" 
       android:layout_height="wrap_content" 
       android:layout_weight="1.0" 
       android:background="#ffffff" 
       android:text="YES" /> 

      <Button 
       android:id="@+id/noButton" 
       android:layout_width="0.0dip" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="5dp" 
       android:layout_weight="1.0" 
       android:background="#ffffff" 
       android:text="NO" /> 
     </LinearLayout> 

    </LinearLayout> 
+3

आपको यह बताना चाहिए कि आपने वास्तव में क्या बदला है। – wsgeorge

10

नहीं संवाद कक्षा उपयोग करते हैं, यह कक्षा का प्रयास करें:

public class MainActivity extends Activity { 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 


        ShowDialog(); 

     } 

     private void ShowDialog() { 
       // TODO Auto-generated method stub 
       final Dialog dialog = new Dialog(context, 
         android.R.style.Theme_Translucent_NoTitleBar); 
       dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
       dialog.setContentView(R.layout.custom_dialog); 
       WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); 
       lp.copyFrom(dialog.getWindow().getAttributes()); 
       lp.width = WindowManager.LayoutParams.MATCH_PARENT; 
       lp.height = WindowManager.LayoutParams.WRAP_CONTENT; 
       lp.gravity = Gravity.CENTER; 

       dialog.getWindow().setAttributes(lp); 

       Button yes = (Button) dialog.findViewById(R.id.yesButton); 
       yes.setOnClickListener(new OnClickListener() { 

        @Override 
        public void onClick(View v) { 
         // TODO Auto-generated method stub 

         dialog.dismiss(); 

        } 
       }); 

       Button no = (Button) dialog.findViewById(R.id.noButton); 
       no.setOnClickListener(new OnClickListener() { 

        @Override 
        public void onClick(View v) { 
         // TODO Auto-generated method stub 
         Intent i = new Intent(
           Intent.ACTION_PICK, 

         dialog.dismiss(); 

        } 
       }); 

        TextView content =(TextView) dialog.findViewById(R.id.txt_dia); 



       dialog.show(); 
      } 
} 
+1

'dialog.getWindow में this.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); रखो() setAttributes (एलपी);।' 'Dialog.show के बाद स्थापित करने की जरूरत है()' एक आकर्षण की तरह काम करता है – Doomsknight

+0

, धन्यवाद! – karenms

0

आप अपने LinearLayout के लिए एक android:gravity="center" जोड़ सकता है और उसके बाद LinearLayout पर एक न्यूनतम ऊंचाई के साथ-साथ जैसे निर्धारित करते हैं, android:minHeight="120dp"। इसके बाद आप विभिन्न उपकरणों पर अपने लेआउट का परीक्षण और यह tweak जब तक आप कुछ है कि सभी आकार की स्क्रीन पर काम करता है प्राप्त कर सकते हैं:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:minHeight="120dp" 
android:gravity="center" 
android:background="#D24379" 
android:orientation="vertical" > 
2

वर्ग का विस्तार संवाद में:

public MyPopUp(MainActivity activity, String sPrm) { 
    super(activity); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.alert_dialog); 
    LinearLayout bedLcl = (LinearLayout) findViewById(R.id.bed); 
    TextView textLcl = (TextView) findViewById(R.id.dialog_text); 
    textLcl.setText(sPrm); 
    setOnCancelListener(this); 
    DisplayMetrics displayMetrics = new DisplayMetrics(); 
activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); 
    int widthLcl = (int) (displayMetrics.widthPixels*0.9f); 
    int heightLcl = (int) (displayMetrics.heightPixels*0.9f); 
    FrameLayout.LayoutParams paramsLcl = (FrameLayout.LayoutParams) 
    bedLcl.getLayoutParams(); 
    paramsLcl.width = widthLcl; 
    paramsLcl.height =heightLcl ; 
    paramsLcl.gravity = Gravity.CENTER; 
    show(); 
    Window window = getWindow(); 
    bedLcl .setLayoutParams(paramsLcl); 
    window .setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));} 

enter image description here

सभी के लिए

सही ढंग से है creens।

1

अपने कस्टम संवाद के onCreate()

संबंधित मुद्दे