如何使dialogFragment宽度与父级匹配?

The*_*heQ 1 xml android android-layout android-xml android-dialogfragment

我有这个dialog fragmentt,我有两个问题.

1.如何使宽度与父母相匹配(最干净,最好的解决方案).

  1. dialog fragment我有一个editText.当对话框片段打开时,如何让它弹出软键盘?

希望你们能帮忙!

这是我的对话框片段java代码:

 @Nullable
    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        rootView  = inflater.inflate(R.layout.activity_13g_add_comment, container, false);//The container is the rootView.

        myCognito = new MyCognito(getActivity().getApplicationContext(),getActivity().getBaseContext());

        cardClient = myCognito.getCardClient();

        bindActivity();

        return rootView;
    }

    private void bindActivity()
    {
        doneButton = (ImageButton) rootView.findViewById(R.id.add_comment_doneButton);
        doneButton.setVisibility(View.GONE);

        imageView = (ImageView) rootView.findViewById(R.id.add_comment_IV);

        RemoveGlideCacheAsyncTask removeGlideCacheAsyncTask = new RemoveGlideCacheAsyncTask(getActivity().getBaseContext(),Global_Class.getInstance().getValue().user.getUsername());
        removeGlideCacheAsyncTask.execute();


        editText = (EditText) rootView.findViewById(R.id.add_comment_ET);

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count)
            {
                enableSubmitButton();
            }

            @Override
            public void afterTextChanged(Editable s)
            {

            }
        });

        imageView = (ImageView) rootView.findViewById(R.id.add_comment_IV);

        doneButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {

            }
        });
    }

    private void enableSubmitButton()
    {
        boolean isReady = editText.getText().toString().length() > 0;
        if(isReady)
        {
            doneButton.setVisibility(View.VISIBLE);
        }
        else
        {
            doneButton.setVisibility(View.GONE);
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="10dp">
        <de.hdodenhof.circleimageview.CircleImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:id="@+id/add_comment_IV"/>
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="Add a comment"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:textColor="@color/black"
            android:id="@+id/add_comment_ET"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="5dp"
            android:background="@android:color/transparent"
            android:maxLength="140"/>
        <ImageButton
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@color/green_main"
            android:id="@+id/add_comment_doneButton"/>
    </LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

arj*_*jun 13

打开DialogFragment如下

SampleDialogFragment sampleDialogFragment = new SampleDialogFragment();
SampleDialogFragment.setStyle(DialogFragment.STYLE_NO_FRAME, 0);
SampleDialogFragment.show(getActivity().getSupportFragmentManager(), "sometag");
Run Code Online (Sandbox Code Playgroud)

然后在DailogFragment覆盖onStart()方法如下

@Override
public void onStart() {
  super.onStart();
  getDialog().getWindow()
         .setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
}
Run Code Online (Sandbox Code Playgroud)

然后显示软键盘试试这个

((InputMethodManager) sampleedittext.getContext()
        .getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(
        sampleedittext, InputMethodManager.SHOW_IMPLICIT);
Run Code Online (Sandbox Code Playgroud)

或者您可以为其创建自定义样式 Dialog

<style name="CustomDialog" parent="AppTheme" >
  <item name="android:windowNoTitle">true</item>
  <item name="android:windowFullscreen">true</item>
  <item name="android:windowIsFloating">true</item>
  <item name="android:windowCloseOnTouchOutside">true</item>
</style>
Run Code Online (Sandbox Code Playgroud)

然后在对话框片段中使用该样式

@Override public void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setStyle(DialogFragment.STYLE_NORMAL, R.style.CustomDialog);
}

@Override public void onStart() {
  super.onStart();
  getDialog().getWindow()
    .setLayout(WindowManager.LayoutParams.MATCH_PARENT,
        WindowManager.LayoutParams.MATCH_PARENT);
}

SampleDialogFragment sampleDialogFragment = new SampleDialogFragment();
SampleDialogFragment.show(getActivity().getSupportFragmentManager(), "sometag");
Run Code Online (Sandbox Code Playgroud)


Pat*_*min 0

使用 appcompactDialog 并应用以下主题:

 <style name="dialogFullScreen" parent="@android:style/Theme.NoTitleBar.Fullscreen">

    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowCloseOnTouchOutside">false</item>
    <item name="android:windowFullscreen">true</item>

    <item name="android:windowNoTitle">true</item>

</style>
Run Code Online (Sandbox Code Playgroud)

下面是java代码示例:

 AppCompatDialog dialogVideoView = new AppCompatDialog(context,R.style.dialogFullScreen);
 dialogVideoView.setContentView(R.layout.custom_dialog_video_surface);
 dialogVideoView.show();
Run Code Online (Sandbox Code Playgroud)