2013-10-02 4 views
6

У меня есть активность android, которая реализует пользовательский диалог. Приложение работает нормально, но диалог слишком мал, я хочу отобразить больший диалог. Как я могу это достичь? Вот мой макет XMLПользовательский диалог слишком мал

<?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="match_parent" 
android:background="@color/white" 
android:orientation="vertical" > 

     <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:background="@drawable/view_more_borders"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:orientation="horizontal" 
      android:layout_marginTop="4dp" 
      android:gravity="center_vertical"> 

      <TextView 
       android:id="@+id/share_amount" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:text="Company Name:" 
       android:textAppearance="?android:attr/textAppearanceSmall" 
       android:textColor="@color/nice_blue" 
       android:typeface="sans" /> 

      <TextView 
       android:id="@+id/textViewCompanyName" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1.80" 
       android:text=" " 
       android:textAppearance="?android:attr/textAppearanceSmall" /> 

     </LinearLayout> 


     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:layout_marginTop="4dp" 
      android:gravity="center_vertical"> 

      <TextView 
       android:id="@+id/textView5" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:text="Price per share:" 

       android:textAppearance="?android:attr/textAppearanceSmall" 
       android:textColor="@color/nice_blue" /> 

      <TextView 
       android:id="@+id/textViewprice_pershare" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="2" 
       android:layout_alignBaseline="@+id/Yesterday" 
       android:layout_alignBottom="@+id/Yesterday" 
       android:layout_marginLeft="38dp" 
       android:fontFamily="sans-serif" 
       android:layout_toRightOf="@+id/company" 
       android:text=" " 
       android:textAppearance="?android:attr/textAppearanceSmall" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:layout_marginTop="4dp" 
      android:gravity="center_vertical"> 

       <TextView 
        android:id="@+id/textView4" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:text="Total cost:" 

        android:textAppearance="?android:attr/textAppearanceSmall" 
        android:textColor="@color/nice_blue" /> 

      <TextView 
       android:id="@+id/textview_totalcost" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="2" 
       android:layout_alignBaseline="@+id/Yesterday" 
       android:layout_alignBottom="@+id/Yesterday" 
       android:layout_marginLeft="38dp" 
       android:fontFamily="sans-serif" 
       android:layout_toRightOf="@+id/company" 
       android:text=" " 
       android:textAppearance="?android:attr/textAppearanceSmall" /> 
     </LinearLayout> 


     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:layout_marginTop="4dp" 
      android:gravity="center_vertical"> 

       <TextView 
      android:id="@+id/textView3" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:text="Number of shares:" 

        android:textAppearance="?android:attr/textAppearanceSmall" 
        android:textColor="@color/nice_blue" /> 

      <EditText 
       android:id="@+id/shareNumber" 
       android:layout_width="0dp" 
       android:layout_weight="2" 
       android:layout_height="wrap_content" 
       android:inputType="number" 
       android:fontFamily="sans-serif" 
       android:textSize="12sp" 
       > 

      <requestFocus /> 
      </EditText> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:layout_marginTop="4dp" 
      android:gravity="center_vertical"> 


       <TextView 
     android:id="@+id/textView2" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:text="Payment method:" 

       android:textAppearance="?android:attr/textAppearanceSmall" 
       android:textColor="@color/nice_blue" /> 

      <Spinner 
       android:id="@+id/spinner_paymentmode" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="2" /> 

     </LinearLayout> 
    </LinearLayout> 
    <Button 
    android:id="@+id/button_buy_shares" 
    style="@style/ButtonText" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/blue_button" 
    android:text="Buy" /> 

А вот мой output.I хочет, чтобы увеличить ширину и высоту окна. enter image description here

И мой файл ява

public class MyDialogFragment extends DialogFragment { 

public MyDialogFragment() { 

} 

private EditText mEditText; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.dialogfragment, container); 
    // mEditText = (EditText) view.findViewById(R.id.txt_your_name); 
    // getDialog().setTitle("Hello"); 
    getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); 

    return view; 
} 
} 
+0

сообщение файл Java. вы должны установить ширину и высоту вашего диалога в java-файле. – Andrain

+0

@ Аndrain я разместил его. –

+0

@mungaihkamau Вы решили свою проблему? – JJ86

ответ

23

Измените свой диалог измерения во время выполнения:

yourDialog.show(); 
yourDialog.getWindow().setLayout((6 * width)/7, LayoutParams.WRAP_CONTENT); 

Вы можете сделать это как для измерения, в моем примере я только изменил ширину. Надеюсь, это поможет!

EDIT

Я забыл упомянуть, где я взял ширина:

DisplayMetrics metrics = getResources().getDisplayMetrics(); 
int width = metrics.widthPixels; 
int height = metrics.heightPixels; 

EDIT 2

Попробуйте этот код:

Dialog yourDialog = dialogFragment.getDialog(); 
yourDialog.getWindow().setLayout((6 * width)/7, (4 * height)/5); 
+0

как у вас была инициализированная ширина? –

+0

@mungaihkamau извините, я забыл фрагмент кода, проверьте правильность! – JJ86

+0

Спасибо JaAd, теперь он работает отлично. Спасибо. –

1

Диалоги имеют размеры по своему содержанию. Вы можете добавить отступы и поля на внешнем макете, чтобы потреблять больше места, но это не будет распространять виды внутри.

0

Тест

Для настройки диалога ширину и высоту в соответствии с устройством размером экрана

Display display; 
    int DisplayWidth, DisplayHeight, DialogWidth, DialogHeight; 
    Dialog dialog; 

display =((WindowManager)activity_context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
     DisplayWidth = display.getWidth(); 
     DisplayHeight = display.getHeight(); 

     if(DisplayHeight > DisplayWidth) 
     { 
      DialogWidth = (6 * DisplayWidth)/7 ; 
      DialogHeight = (4 * DisplayHeight)/5 ; 
     } 
     else 
     { 
      DialogWidth = (6 * DisplayWidth)/9 ; 
      DialogHeight = (4 * DisplayHeight)/5 ; 
     } 

     dialog = new Dialog(activity_context); 

      // Set your dialog width and height dynamically as per your screen. 

     Window window = dialog.getWindow(); 
     window.setLayout(DialogWidth,DialogHeight); 
     window.setGravity(Gravity.CENTER); 

     dialog.show(); 
+0

Вы хотели бы помочь мне изменить мой код в соответствии с вашим решением? –

+0

@ Андриан не работает. Имеет несколько ошибок. –

3
<style name="full_screen_dialog" parent="@android:style/Theme.Dialog"> 
    <item name="android:windowNoTitle">true</item> 
    <item name="android:windowFullscreen">true</item> 
    <item name="android:windowIsFloating">true</item> 
    <item name="android:windowContentOverlay">@null</item> 
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item> 
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item> 
    <item name="android:windowBackground">@android:color/transparent</item> 
    <item name="android:minWidth" type="dimen">600dp</item> 
</style> 

использовать это в style.xml файл и использовать этот стиль в диалоговом классе

+0

Единственное, что работает для меня здесь, это minWidth = 600dp. В моем случае мне не нужны другие элементы стиля –

6

Установка андроид: MinWidth и Android: MinHeight в обычае макет делает трюк для меня.

0

пытаются установить android:layout_height до некоторого значения для базы LinearLayout

, например

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 

<!-- right here --> 
    android:layout_height="100dp" 

android:background="@color/white" 
android:orientation="vertical" > 
0

Добавить android: minWidth/android: minHeight к вашему корневому представлению.Смотрите пример ниже:

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:minWidth="300dp" 
android:minHeight="500dp"> 
1

положить ниже линии в OnCreate

setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Holo_Light_Dialog_NoActionBar_MinWidth); 

original answer

Смежные вопросы