边距属性在自定义DialogFragment中不起作用

时间:2013-11-26 06:24:15

标签: android android-layout android-fragments customdialog

我在为自定义DialogFragment视图定义的布局中有一些EditText。我把边距放在他们的右边。但它在屏幕上呈现DialogFragment时没有反映出来。

login_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/lightBlack" >

    <Button
        android:id="@+id/loginDialogCloseButton"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@drawable/btn_dialog_disable"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="10dp"
        android:layout_marginRight="20dp"


        />

    <TextView 
        android:id="@+id/loginNameTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="NAME"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="10dp"
        android:layout_alignBottom="@+id/loginNameEditText"

        android:layout_marginBottom="10dp"
        android:textStyle="bold"
        android:textColor="@android:color/darker_gray"        
        />

    <EditText
        android:id="@+id/loginNameEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/loginPasswordEditText"
        android:layout_alignParentRight="true"
        android:layout_marginRight="20dp"
        android:layout_below="@+id/loginDialogCloseButton"

        android:layout_marginTop="20dp"
        android:layout_toRightOf="@+id/loginNameTextView" />

     <TextView 
        android:id="@+id/loginPasswordTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="PASSWORD"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="10dp"
        android:layout_alignBottom="@+id/loginPasswordEditText"        
        android:layout_marginBottom="10dp"
        android:textStyle="bold"
        android:textColor="@android:color/darker_gray"        
        />
    <EditText
        android:id="@+id/loginPasswordEditText"
        android:inputType="textPassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/loginNameEditText"
        android:layout_toRightOf="@+id/loginPasswordTextView"
        android:layout_alignParentRight="true"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"

        android:layout_marginLeft="10dp"
        />

    <Button
        android:id="@+id/loginButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/loginPasswordEditText"
        android:layout_below="@+id/loginPasswordEditText"
        android:layout_marginTop="20dp"
        android:text="LOGIN"
        android:gravity="center"
        android:textColor="@color/lightBlack"
        android:textStyle="bold" />

</RelativeLayout>

LoginDialog.java

@SuppressLint("ValidFragment")
public class LoginDialog extends DialogFragment {

    //*******interface for communicating with the host********//
    public interface LoginCommunicator{
        public void getLoginCredentials(String name,String password);

    }

    LoginCommunicator listener;

    //******default constructor*********************//
    public LoginDialog() {
        // TODO Auto-generated constructor stub
    }

    public LoginDialog(FragmentSettings fragmentSettings) {
        // TODO Auto-generated constructor stub
        listener=fragmentSettings;
    }

    /*//********from stackOverflow: it's safer to add the request for
    a title-less window in the onCreateDialog() method instead of
    onCreateView(). This way you can prevent ennoying NPE when
    you're not using your fragment as a Dialog
*/  
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        Dialog dialog= super.onCreateDialog(savedInstanceState);

        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);

        return dialog;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view=inflater.inflate(R.layout.login_dialog, container);
    //  getDialog().setTitle("LOGIN");

    //  getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        final EditText loginNameEditText=(EditText)view.findViewById(R.id.loginNameEditText);
        final EditText loginPasswordEditText=(EditText)view.findViewById(R.id.loginPasswordEditText);

        Button loginButton=(Button) view.findViewById(R.id.loginButton);
        loginButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                String name=loginNameEditText.getText().toString();
                String password=loginPasswordEditText.getText().toString();

                listener.getLoginCredentials(name, password);
            }
        });
        return view;
    }
}

查看已呈现:

enter image description here

因此我认为android:layout_marginRight="20dp"没有改变。如何实现这一变化?

1 个答案:

答案 0 :(得分:15)

在某些情况下,非常外部的margin在某些Android API版本上无效或被降至零。它主要发生在AdapterViewDialog中。 如果您使用padding,则可以使用所有Android版本。