簡體   English   中英

如何使dialogFragment寬度與父對象匹配?

[英]How to make dialogFragment width match parent?

我有這個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);
        }
    }

這是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>

如下所示打開DialogFragment

SampleDialogFragment sampleDialogFragment = new SampleDialogFragment();
SampleDialogFragment.setStyle(DialogFragment.STYLE_NO_FRAME, 0);
SampleDialogFragment.show(getActivity().getSupportFragmentManager(), "sometag");

然后在DailogFragment重寫onStart()方法,如下所示

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

然后顯示軟鍵盤試試這個

((InputMethodManager) sampleedittext.getContext()
        .getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(
        sampleedittext, InputMethodManager.SHOW_IMPLICIT);

或者您可以為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>

然后在對話框片段中使用該樣式

@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");

使用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>

以下是Java代碼示例:

 AppCompatDialog dialogVideoView = new AppCompatDialog(context,R.style.dialogFullScreen);
 dialogVideoView.setContentView(R.layout.custom_dialog_video_surface);
 dialogVideoView.show();

對於自定義對話框,可以使用:

Dialog dialog_guest = new Dialog(MainActivity.this);

dialog_guest.requestWindowFeature(Window.FEATURE_NO_TITLE);

dialog_guest.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

dialog_guest.setContentView(R.layout.yourxml);

dialog_guest.setCanceledOnTouchOutside(false);
        dialog_guest.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

dialog_guest.show();

我也認為您的鍵盤會自動打開。 如果沒有,請使用:

InputMethodManager inputMethodManager =  (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
        inputMethodManager.toggleSoftInputFromWindow(yourEditText.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
        yourEditText.requestFocus();

對於第一個問題,您可以在style.xml中將其添加到dialog style

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

另外,您必須讓EditText requestFocus並顯示鍵盤,這是代碼:

edit.requestFocus();
InputMethodManager imm = (InputMethodManager) edit.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED); 

希望能幫助到你。

只需在片段的布局內部創建一個具有很大高度和寬度的視圖:

<View
    android:layout_width="1600dp"
    android:layout_height="1600dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

然后在DialogFragment的onCreateView方法中添加以下代碼:

if (getDialog() != null && getDialog().getWindow() != null) {
    getDialog().getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
    getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}

經過ConstraintLayout測試(因此它應該適用於所有類型的布局)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM