如何将对话窗口包装到内容?

时间:2014-09-16 19:16:44

标签: android android-layout android-alertdialog android-dialog android-windowmanager

我正在开发一个Dialog,它应缩小到其内容,因此,我希望在一个公共视图中获得wrap_content之类的行为,但是对于正常的Dialog。 这就是我想在对话窗口中显示的内容

Layout

在对话窗口中应该如下所示 Desired behaviour

但这是我实际得到的 got behaviour

你能说我做错了吗?

我提前感谢你。

这是我的代码

...
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
dialog.setContentView(R.layout.advanced_options);
dialog.setTitle(titleId);

// Stuff referred to builder
AlertDialog.Builder builder ...
...

int type = WindowManager.LayoutParams.TYPE_INPUT_METHOD;
WindowManager.LayoutParams w_layout_params = new WindowManager.LayoutParams(type);

dialog.getWindow().setAttributes(w_layout_params );
...
builder.create();
dialog.show();

advanced_options.xml布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relLayout_advancedOptions"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@drawable/border_advanced_options"
    android:divider="?android:listSeparatorTextViewStyle"
    android:showDividers="" >    

    <TextView
        android:id="@+id/speed_limit"
        style="?android:attr/listSeparatorTextViewStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="20dp"
        android:text="@string/speed_limit"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/black"
        android:textSize="14dp" />

    <TextView
        android:id="@+id/speed_limit_alert"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/speed_limit"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="20dp"
        android:text="@string/speed_limit_alert"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/black" />


    <TextView
        android:id="@+id/percent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/speed_limit_alert"
        android:layout_toRightOf="@+id/speed_limit_alert_edit_text"
        android:text="@string/percentage_symbol"
        android:textColor="@color/black" />

    <TextView
        android:id="@+id/minimum_speed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/speed_limit_alert"
        android:layout_below="@+id/speed_limit_alert_edit_text"
        android:layout_marginTop="15dp"
        android:text="@string/minimum_speed"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/black" />

    <EditText
        android:id="@+id/minimum_speed_edit_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/minimum_speed"
        android:layout_alignBottom="@+id/minimum_speed"
        android:layout_alignLeft="@+id/speed_limit_alert_edit_text"
        android:ems="3"
        android:inputType="number"
        android:textColor="@color/black"
        android:text="6" />

    <TextView
        android:id="@+id/minimum_speed_unit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/minimum_speed"
        android:layout_alignLeft="@+id/percent"
        android:text="@string/metric_speed_unit"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/black" />

    <TextView
        android:id="@+id/maximum_speed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/minimum_speed"
        android:layout_below="@+id/minimum_speed_edit_text"
        android:layout_marginTop="15dp"
        android:text="@string/maximum_speed"
        android:textAppearance="?android:attr/textAppearanceSmall" 
        android:textColor="@color/black"/>

    <EditText
        android:id="@+id/maximum_speed_edit_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/maximum_speed"
        android:layout_alignBottom="@+id/maximum_speed"
        android:layout_alignLeft="@+id/minimum_speed_edit_text"
        android:ems="3"
        android:inputType="number"
        android:textColor="@color/black"
        android:text="18" />

    <TextView
        android:id="@+id/maximum_speed_unit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/maximum_speed"
        android:layout_toRightOf="@+id/maximum_speed_edit_text"
        android:text="@string/metric_speed_unit"
        android:textAppearance="?android:attr/textAppearanceSmall" 
        android:textColor="@color/black"/>

    <TextView
        android:id="@+id/mobile_device_performance"
        style="?android:attr/listSeparatorTextViewStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/maximum_speed_edit_text"
        android:layout_marginTop="20dp"
        android:gravity="center_vertical"
        android:text="@string/mobile_device_performance"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/black"
        android:textSize="14dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/maximum_number_objects"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/maximum_speed"
        android:layout_below="@+id/mobile_device_performance"
        android:layout_marginTop="20dp"
        android:text="@string/maximum_objects_in_view"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/black" />

    <EditText
        android:id="@+id/maximum_number_objects_edit_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/maximum_number_objects"
        android:layout_alignBottom="@+id/maximum_number_objects"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/maximum_number_objects"
        android:ems="3"
        android:inputType="number"
        android:text="450"
        android:textColor="@color/black" />

    <EditText
        android:id="@+id/speed_limit_alert_edit_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/percent"
        android:layout_alignBottom="@+id/percent"
        android:layout_alignLeft="@+id/maximum_number_objects_edit_text"
        android:ems="3"
        android:inputType="number"
        android:text="42"
        android:textColor="@color/black" >

        <requestFocus />
    </EditText>

    <View
        android:id="@+id/void_view"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/maximum_speed_unit"
        android:layout_below="@+id/maximum_number_objects" />

2 个答案:

答案 0 :(得分:2)

我可以将Dialog(NOT AlertDialog!)调整为我的自定义布局,但我无法设置标题或按钮,以便我在requestFeature之前设置内容的异常原因,然后我决定自定义我的对话框,并在我的自定义布局中包含按钮和标题。

*我已经研究过了,如果我们在OnCreate中定义了对话,甚至在OnCreateDialog中也可以克服它,但是我只有静态对话方法的额外类,对我来说没有解决方案。

另外,我发布了对我有用的内容,也许有人也很有用。

现在我的自定义对话框看起来像这样

enter image description here

这里是想要测试它的代码。

/**
 * @param c
 *            the Context
 * @return the about dialog
 */
public static void getAdvancedOptions(final Activity activity) {

    Log.i("TAG", "Dialogs::getAdvancedOptions:: 0");
    //settings            = session.getSettings();

    // Creation of a Dialog with Frame and title Characteristics
    final Dialog dialog = new Dialog(activity);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.advanced_options_expanded);


    // Here we control the validity of "edit_text" Fields
    speed_limit_alert   = (EditText)dialog.findViewById(R.id.speed_limit_alert_edit_text);                                          
    minimum_speed       = (EditText)dialog.findViewById(R.id.minimum_speed_edit_text);                
    maximum_speed       = (EditText)dialog.findViewById(R.id.maximum_speed_edit_text);               
    max_objects_in_view = (EditText)dialog.findViewById(R.id.maximum_number_objects_edit_text);

    //showAdvancedOptions("getAdvancedOptions 1::");

    // First we set the "SharedPreferences"-saved values on EditText Fields                
    performEditText(speed_limit_alert,Constants.MIN_PERCENTAGE, Constants.MAX_PERCENTAGE,"speed_limit_alert"); 
    performEditText(minimum_speed, "","", "minimum_speed"); 
    performEditText(maximum_speed, "","","maximum_speed"); 
    performEditText(max_objects_in_view, "","","max_objects_in_view");  

    //showAdvancedOptions("getAdvancedOptions 4::");

    ok_button     = (Button)dialog.findViewById(R.id.ok_button);                                        
    cancel_button = (Button)dialog.findViewById(R.id.cancel_button);  


    // Definition of "OK" Button for the Dialog     
    ok_button.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            Log.i("TAG", "Dialogs::getAdvancedOptions::onClick");
            if ( dialog != null && dialog.isShowing()) {
                dialog.dismiss();
            }

        }
    });     

    cancel_button.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            Log.i("TAG", "Dialogs::getAdvancedOptions::onClick");
            if ( dialog != null && dialog.isShowing()) {
                dialog.dismiss();
            }

        }
    }); 

    dialog.show();

}

private static void performEditText(EditText edit_text, final String min_value, final String max_value, final String id) {

    String edit_text_value = edit_text.getText().toString();
    Log.i("TAG", "Dialogs::performEditText:: id: "+ id +" edit_text_value: "+edit_text_value+ " (min_value,max_value)=("+min_value+","+max_value+")");

    TextWatcher textWatcher = new TextWatcher() {
        public void afterTextChanged(Editable s) {
            int i = 0;
            Log.i("TAG","Dialogs::performEditText::afterTextChanged:id: "+ id +" - 0 : s: "+s.toString());

            int length = s.length();

            if ( length == 0 )
                i = 0;
            else if ( length < 3 ) {
                    if ( s.charAt(0) == '0')
                        s.delete(1, length );

                    i = Integer.parseInt(s.toString());
                    Log.i("TAG","Dialogs::performEditText::afterTextChanged:id: "+ id +" - 1a : s: "+s+", i:"+i);
            } else {
                //String hundred = "100";

                String s_value = s.toString();
                Log.i("TAG","Dialogs::performEditText::afterTextChanged: id: "+ id +" - 1b : s: "+s+", i:"+i);

                if ( ( min_value != null ) && ( min_value.length() != 0 ) && ( max_value != null) && ( max_value.length() != 0 )) {


                    if ( !s_value.equalsIgnoreCase(max_value))
                        s.delete(2, length );

                }

                i = Integer.parseInt(s.toString());
                Log.i("TAG","Dialogs::performEditText::afterTextChanged: id: "+ id +" - 2b : i: "+i);                   
            }
            if (i >= 0 && i <= 100) {
                Log.i("TAG","Dialogs::performEditText::afterTextChanged: id: "+ id +" - 3 : (i >= 0 && i <= 100): i: "+i);
                //speed_limit_alert.setText(s); // This ensures 0-100 value for speed_limit_alert
            }               
            Log.i("TAG","Dialogs::performEditText::afterTextChanged: id: "+ id +" - 4 : i: "+i);

        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            Log.i("TAG","Dialogs::performEditText::beforeTextChanged: id: "+ id +" s: "+s); 
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            Log.i("TAG","Dialogs::performEditText::onTextChanged: id: "+ id +" s: "+s);
        }
    };


    edit_text.addTextChangedListener(textWatcher);  


}



private static void showAdvancedOptions(String entryPoint) {
    String s_l_a = speed_limit_alert.getText().toString();  
    Log.i("TAG", entryPoint + "Dialogs::setAdvancedOptions:: s_l_a: "+s_l_a);
    String mi_s = minimum_speed.getText().toString();   
    Log.i("TAG", entryPoint + "Dialogs::setAdvancedOptions:: mi_s: "+mi_s);
    String ma_s = maximum_speed.getText().toString();   
    Log.i("TAG", entryPoint + "Dialogs::setAdvancedOptions:: ma_s: "+ma_s);
    String m_o_i_v = max_objects_in_view.getText().toString();  
    Log.i("TAG", entryPoint + "Dialogs::setAdvancedOptions:: m_o_i_v: "+m_o_i_v);       

}

布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@drawable/border_advanced_options"
    android:divider="?android:listSeparatorTextViewStyle"
    android:showDividers="middle" >  

<RelativeLayout
    android:id="@+id/relLayout_advancedOptions"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:divider="?android:listSeparatorTextViewStyle"
    android:orientation="vertical"
    android:showDividers="" >

    <TextView
        android:id="@+id/speed_limit"
        style="?android:attr/listSeparatorTextViewStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/void_view_0"
        android:layout_marginTop="10dp"
        android:text="@string/speed_limit"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/black"
        android:textSize="14dp" />

    <TextView
        android:id="@+id/speed_limit_alert"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/speed_limit"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:text="@string/speed_limit_alert"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/black" />

    <TextView
        android:id="@+id/percent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/speed_limit_alert"
        android:layout_toRightOf="@+id/speed_limit_alert_edit_text"
        android:text="@string/percentage_symbol"
        android:textColor="@color/black" />

    <TextView
        android:id="@+id/minimum_speed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/speed_limit_alert"
        android:layout_below="@+id/speed_limit_alert_edit_text"
        android:layout_marginTop="15dp"
        android:text="@string/minimum_speed"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/black" />

    <EditText
        android:id="@+id/minimum_speed_edit_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/minimum_speed"
        android:layout_alignBottom="@+id/minimum_speed"
        android:layout_alignLeft="@+id/speed_limit_alert_edit_text"
        android:ems="3"
        android:inputType="number"
        android:text="6"
        android:textColor="@color/black" />

    <TextView
        android:id="@+id/minimum_speed_unit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/minimum_speed"
        android:layout_alignLeft="@+id/percent"
        android:paddingRight="5dp"
        android:text="@string/metric_speed_unit"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/black" />

    <TextView
        android:id="@+id/maximum_speed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/minimum_speed"
        android:layout_below="@+id/minimum_speed_edit_text"
        android:layout_marginTop="15dp"
        android:text="@string/maximum_speed"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/black" />

    <EditText
        android:id="@+id/maximum_speed_edit_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/maximum_speed"
        android:layout_alignBottom="@+id/maximum_speed"
        android:layout_alignLeft="@+id/minimum_speed_edit_text"
        android:ems="3"
        android:inputType="number"
        android:text="18"
        android:textColor="@color/black" />

    <TextView
        android:id="@+id/maximum_speed_unit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/maximum_speed"
        android:layout_alignRight="@+id/minimum_speed_unit"
        android:layout_toRightOf="@+id/maximum_speed_edit_text"
        android:paddingRight="5dp"
        android:text="@string/metric_speed_unit"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/black" />

    <TextView
        android:id="@+id/mobile_device_performance"
        style="?android:attr/listSeparatorTextViewStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/maximum_speed_edit_text"
        android:layout_marginTop="20dp"
        android:gravity="center_vertical"
        android:text="@string/mobile_device_performance"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/black"
        android:textSize="14dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/maximum_number_objects"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/maximum_speed"
        android:layout_below="@+id/mobile_device_performance"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:text="@string/maximum_objects_in_view"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/black" />

    <EditText
        android:id="@+id/maximum_number_objects_edit_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/maximum_number_objects"
        android:layout_alignBottom="@+id/maximum_number_objects"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/maximum_number_objects"
        android:ems="3"
        android:inputType="number"
        android:text="450"
        android:textColor="@color/black" />

    <EditText
        android:id="@+id/speed_limit_alert_edit_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/percent"
        android:layout_alignBottom="@+id/percent"
        android:layout_alignLeft="@+id/maximum_number_objects_edit_text"
        android:digits="0123456789"
        android:ems="3"
        android:inputType="number"
        android:text="42"
        android:textColor="@color/black" >

        <requestFocus />
    </EditText>

    <View
        android:id="@+id/void_view"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/lin_layout_buttons"
        android:layout_below="@+id/maximum_number_objects"
        android:background="@color/black" />

    <TextView
        android:id="@+id/title_advanced_options"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/minimum_speed_unit"
        android:drawableLeft="@drawable/ic_launcher_48"
        android:drawableRight="@drawable/ic_action_settings_48"
        android:gravity="center|center_vertical"
        android:paddingLeft="5dp"
        android:text="Advanced Options"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/blue" />

    <View
        android:id="@+id/void_view_0"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/title_advanced_options"
        android:layout_below="@+id/title_advanced_options"
        android:background="@color/black" />
    <LinearLayout
        android:id="@+id/lin_layout_buttons"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/minimum_speed_unit"
        android:layout_alignTop="@+id/void_view"
        android:weightSum="3" >

        <Button
            android:id="@+id/ok_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/maximum_number_objects"
            android:layout_alignRight="@+id/speed_limit_alert"
            android:layout_below="@+id/maximum_number_objects_edit_text"
            android:layout_weight="1.5"
            android:text="OK" />

        <Button
            android:id="@+id/cancel_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/button1"
            android:layout_alignBottom="@+id/button1"
            android:layout_toRightOf="@+id/button1"
            android:layout_weight="1.5"
            android:text="Cancel" />
    </LinearLayout>     
</RelativeLayout>

答案 1 :(得分:1)

RelativeView的宽度更改为match_parent

您正在寻找的是match_parent的行为。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relLayout_advancedOptions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/border_advanced_options"
android:divider="?android:listSeparatorTextViewStyle"
android:showDividers="" > 
  

注意: wrap_content在android中指定视图用作所需的小空间,这就是您正在获得的行为。