gpt4 book ai didi

android - 调整 DialogFragment 宽度和高度

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:43:10 27 4
gpt4 key购买 nike

我对 DialogFragmntWidthHeight 有疑问。这是我的代表 DialogFragmetn 的类:

public class RecipeAddDialogFragment extends DialogFragment {

private ArrayList<RecipeDialogItem> recipeDialogItems;
private RecipeAddDialogAdapter recipeDialogAdapter;
private String recipeUniqueId;
private CoordinatorLayout coordinatorLayout;
private RecipeAddDialogFragment recipeDialogFragment;

@Override
public void onStart() {
super.onStart();
if (getDialog() == null) {
return;
}

int dialogWidth = 600;
int dialogHeight = 300;
getDialog().getWindow().setLayout(dialogWidth, dialogHeight);
getDialog().setTitle(getString(R.string.recipe_dialog_title));
}

@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setStyle(DialogFragment.STYLE_NORMAL, R.style.AppTheme_DialogFragment);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.dialog_fragment, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
recipeDialogItems = new ArrayList<>();
RecyclerView dialogRecyclerView = (RecyclerView) view.findViewById(
R.id.dialog_recycler_view);

recipeDialogAdapter = new RecipeAddDialogAdapter(getContext(), recipeDialogItems,
R.layout.recipe_dialog_item);
recipeDialogAdapter.setRuidClRdf(recipeUniqueId, coordinatorLayout, recipeDialogFragment);

dialogRecyclerView.setHasFixedSize(true);
dialogRecyclerView.setAdapter(recipeDialogAdapter);
dialogRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
fillRecipeDialogArray();
}

private void fillRecipeDialogArray() {
String name = getString(R.string.add_to_favourites);
int icon = R.drawable.ic_heart_48dp;;

RecipeDialogItem dialogItem = new RecipeDialogItem();
dialogItem.setRowIcon(icon);
dialogItem.setRowOption(name);
recipeDialogItems.add(dialogItem);

recipeDialogAdapter.notifyDataSetChanged();
}

public void setReferences(String recipeUniqueId, CoordinatorLayout coordinatorLayout,
RecipeAddDialogFragment recipeDialogFragment) {
this.recipeUniqueId = recipeUniqueId;
this.coordinatorLayout = coordinatorLayout;
this.recipeDialogFragment = recipeDialogFragment;
}
}

这是我在这个DialogFragment中引入的.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center|left"
android:padding="16dp"
android:orientation="horizontal"
android:clickable="true"
android:background="?android:attr/selectableItemBackground">

<!-- Option Icon -->
<ImageView
android:id="@+id/recipe_dialog_option_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="4dp"
android:tint="@color/primary" />

<!-- Text Option -->
<TextView
android:id="@+id/recipe_dialog_option_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="@color/primary_text" />

</LinearLayout>

问题是,当我将它的大小设置为 600 x 300 时,它在我的 1280x720 设备上显示正常,但是例如当我的 friend 在 1920x1080 分辨率上显示它时,对话框被换行并且只显示标题。列表已换行,并未完整显示。知道如何自动设置它的大小以适应每个显示器并显示包装到它的内容的整个对话框吗?

编辑

我已经想出办法将 DialogFragment 的宽度调整为其内容,如下所示:

getDialog().getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT);
getDialog().setTitle(getString(R.string.recipe_dialog_title));

但是高度不能正常工作:/

最佳答案

在 DialogFragment.onResume() 中:

int width = getResources().getDimensionPixelSize(R.dimen.popup_width);
int height = getResources().getDimensionPixelSize(R.dimen.popup_height);
getDialog().getWindow().setLayout(width, height);

在对话框的布局中:

android:layout_width="match_parent"
android:layout_height="match_parent"

可以使用整个屏幕:

getDialog().getWindow().setLayout(
getResources().getDisplayMetrics().widthPixels,
getResources().getDisplayMetrics().heightPixels
);

希望对你有帮助

在这里找到想法 How to set DialogFragment's width and height?

关于android - 调整 DialogFragment 宽度和高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34143166/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com