Android 如何使dialogFragment宽度与父级匹配?
我有这个Android 如何使dialogFragment宽度与父级匹配?,android,xml,android-layout,android-xml,android-dialogfragment,Android,Xml,Android Layout,Android Xml,Android Dialogfragment,我有这个对话框碎片,我有两个问题 1.如何使宽度匹配父级(请提供最干净和最佳的解决方案) 在对话框片段中我有一个编辑文本。当对话框片段打开时,如何使其弹出软键盘 希望你们能帮忙 以下是我的对话片段java代码: @Nullable @Override public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
对话框碎片
,我有两个问题
1.如何使宽度匹配父级(请提供最干净和最佳的解决方案)
对话框片段中
我有一个编辑文本。当对话框片段打开时,如何使其弹出软键盘 @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>
使用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>
对于自定义对话框,您可以使用:
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();
打开
对话框fragment
,如下所示
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);
或者你可以
为对话框创建自定义样式
<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>
对于第一个问题,您可以在style.xml
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
希望有帮助。只需在片段的布局中创建一个高度和宽度都非常大的视图:
<View
android:layout_width="1600dp"
android:layout_height="1600dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent" />
使用ConstraintLayout
(因此它应该适用于所有类型的布局)。如果我使用SampleDialogFragment.setStyle(DialogFragment.STYLE\u NO\u FRAME,0)代码>然后,对话框片段的背景变得透明。您可以将backgroundcolor
设置为对话框的根视图,或者尝试编辑后的答案
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
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" />
if (getDialog() != null && getDialog().getWindow() != null) {
getDialog().getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}