[英]How to make dialogFragment width match parent?
I have this dialog fragmentt
, and I have two problems. 我有这个dialog fragmentt
,并且有两个问题。
1.How do i make the width match parent (cleanest and best solution please). 1.如何使宽度与父项匹配(请选择最干净,最好的解决方案)。
- In the
dialog fragment
i have an editText. 在dialog fragment
我有一个editText。 How do i make it pop up a soft keyboard when the dialog fragment opens? 对话框片段打开后,如何使其弹出软键盘?
Hope you guys can help! 希望你们能提供帮助!
Here is my dialog fragment java code: 这是我的对话框片段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);
}
}
And here is the xml 这是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>
Open the DialogFragment
as below 如下所示打开DialogFragment
SampleDialogFragment sampleDialogFragment = new SampleDialogFragment();
SampleDialogFragment.setStyle(DialogFragment.STYLE_NO_FRAME, 0);
SampleDialogFragment.show(getActivity().getSupportFragmentManager(), "sometag");
Then in the DailogFragment
override onStart()
method as below 然后在DailogFragment
重写onStart()
方法,如下所示
@Override
public void onStart() {
super.onStart();
getDialog().getWindow()
.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
}
Then to show softkeyboard try this 然后显示软键盘试试这个
((InputMethodManager) sampleedittext.getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(
sampleedittext, InputMethodManager.SHOW_IMPLICIT);
or you can Create a custom style for Dialog
或者您可以为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>
then use that style in dialog fragment 然后在对话框片段中使用该样式
@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");
Use appcompactDialog and apply below theme : 使用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>
Below is java code sample : 以下是Java代码示例:
AppCompatDialog dialogVideoView = new AppCompatDialog(context,R.style.dialogFullScreen);
dialogVideoView.setContentView(R.layout.custom_dialog_video_surface);
dialogVideoView.show();
For custom dialog box you can use: 对于自定义对话框,可以使用:
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();
Also i think your keyboard will automatically open up. 我也认为您的键盘会自动打开。 If not then use: 如果没有,请使用:
InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(yourEditText.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
yourEditText.requestFocus();
For your first question you can add this to your dialog style
in style.xml
对于第一个问题,您可以在style.xml
中将其添加到dialog style
中
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
And for another ,you have to let your EditText
requestFocus and show the keyboard,here is the code: 另外,您必须让EditText
requestFocus并显示键盘,这是代码:
edit.requestFocus();
InputMethodManager imm = (InputMethodManager) edit.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
hope it helps. 希望能帮助到你。
Just create a view inside your fragment's layout with a very large height and width: 只需在片段的布局内部创建一个具有很大高度和宽度的视图:
<View
android:layout_width="1600dp"
android:layout_height="1600dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent" />
and then in the onCreateView
method of the DialogFragment add the following code: 然后在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));
}
Tested with ConstraintLayout
(so it should work on all types of layouts). 经过ConstraintLayout
测试(因此它应该适用于所有类型的布局)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.