我知道这个问题已被问过几次,但我遇到的解决方案都没有为我工作,因此这个话题。正如标题所述 - 我想设置对话框的外边距:
PurchaseDetailsDialogFragment
public class PurchaseDetailsDialogFragment extends DialogFragment {
private static final String MAX_AMOUNT = "maxAmount";
private static final String UNIT_PRICE = "unitPrice";
private static final String PICKUP_TIME_FROM = "pickupTimeFrom";
private static final String PICKUP_TIME_TO = "pickupTimeTo";
public PurchaseDetailsDialogFragment() { }
public static PurchaseDetailsDialogFragment newInstance(int maxAmount, float unitPrice, String pickupTimeFrom, String pickupTimeTo) {
PurchaseDetailsDialogFragment fragment = new PurchaseDetailsDialogFragment();
Bundle args = new Bundle();
args.putInt(MAX_AMOUNT, maxAmount);
args.putFloat(UNIT_PRICE, unitPrice);
args.putString(PICKUP_TIME_FROM, pickupTimeFrom);
args.putString(PICKUP_TIME_TO, pickupTimeTo);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
int maxAmount = getArguments().getInt(MAX_AMOUNT);
float unitPrice = getArguments().getFloat(UNIT_PRICE);
String pickupFrom = getArguments().getString(PICKUP_TIME_FROM);
String pickupTo = getArguments().getString(PICKUP_TIME_TO);
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Context context = getContext();
FragmentPurchaseDetailsDialogBinding binding = DataBindingUtil.inflate(
LayoutInflater.from(context),
R.layout.fragment_purchase_details_dialog,
null,
false);
binding.setDataContext(new PurchaseDetailsViewModel(context));
AlertDialog dialog = new AlertDialog.Builder(getActivity(), R.style.DialogTheme)
.setView(binding.getRoot())
.create();
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
return dialog;
}
}
fragment_purchase_details_dialog
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/dialog">
<data>
<variable
name="dataContext"
type="com.myapp.viewModels.PurchaseDetailsViewModel" />
</data>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="80dp"
android:paddingTop="20dp"
android:layout_centerHorizontal="true"
android:background="@color/white">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/choose_amount"
style="@style/Widget.App.PurchaseTextViewTitle" />
</FrameLayout>
<LinearLayout
android:id="@+id/dialogCentralContent"
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical"
android:layout_marginTop="80dp"
android:background="@color/dirtyWhite">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="74" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="46dp"
android:layout_centerHorizontal="true">
<Button
android:layout_width="44dp"
android:layout_height="44dp"
android:layout_marginTop="6dp"
android:background="@drawable/button_filled"
android:text="-"
style="@style/Widget.App.PurchaseIncDecButton" />
<FrameLayout
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:background="@drawable/edittext_white_rounded">
<EditText
android:layout_width="30dp"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:gravity="center_horizontal"
android:maxLines="1"
style="@style/Widget.App.PurchaseAmountEditText" />
</FrameLayout>
<Button
android:layout_width="44dp"
android:layout_height="44dp"
android:layout_marginTop="6dp"
android:background="@drawable/button_filled"
android:text="+"
style="@style/Widget.App.PurchaseIncDecButton" />
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@+id/dialogCentralContent"
android:text="@string/buttonBuyText"
android:background="@drawable/button_submit"
style="@style/Widget.App.SubmitButton" />
</RelativeLayout>
</layout>
现在仅使用上面的代码,我的对话框占据了屏幕的整个宽度。如果我这样做,在片段java代码中:
AlertDialog dialog = new AlertDialog.Builder(getActivity(), R.style.DialogTheme) /// the rest of the code
添加一个主题:
<resources>
<style name="DialogTheme" parent="@android:style/Theme.Material.Dialog.Alert">
<item name="android:windowMinWidthMajor">380dp</item>
<item name="android:windowMinWidthMinor">380dp</item>
</style>
</resources>
然后发生了一些时髦的事情。在API23上看起来都很好,而在API19及以下(没有检查19和23之间的apis)对话框是100%宽并且对齐到屏幕顶部。如何使它按照我喜欢的方式工作?
答案 0 :(得分:1)
这是一个非常简单的解决方案。只需将布局放在内部,例如FrameLayout
,并在外部布局元素上设置适当的填充。然后所有api的一切看起来都一样:
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/defaultMargin"
android:paddingRight="@dimen/defaultMargin">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</RelativeLayout>
</FrameLayout>
答案 1 :(得分:0)
How to Build AppCompatDialog From AlertDialog.Builder or Equivalent?
它建议您使用(?s)message(.*?)(?:\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-2])|\Z)
^ ^
而不是android.support.v7.app.AlertDialog
因此,对于有多个选项的对话框,请尝试android.app.AlertDialog
AppCompactDialog
我没试过这个。如果有效,请告诉我。
答案 2 :(得分:0)
<item name="android:windowMinWidthMinor">380dp</item>
这将使对话框的宽度最小为380dp,但是如果设备的宽度小于380dp,则对话框将占用整个可用空间
如果您使用宽度较小的设备进行了测试(很可能是如果设备的api较小,例如Galaxy Nexus的宽度为360dp),那么它将占用整个宽度。
最好将minWidth指定为百分比
对于您的特定情况,要获得预定义的边距(无需以编程方式修改Window的布局属性),我们可以将最小宽度设置为100%
<item name="android:windowMinWidthMinor">100%</item>
,并使用内置的可绘制对象作为windowBackground,如果您使用的是AppCompat,则已经存在这种情况
<item name="android:windowBackground">@drawable/abc_dialog_material_background</item>
abc_dialog_material_background:
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="16dp"
android:insetTop="16dp"
android:insetRight="16dp"
android:insetBottom="16dp">
<shape android:shape="rectangle">
<corners android:radius="@dimen/abc_dialog_corner_radius_material" />
<solid android:color="@android:color/white" />
</shape>
</inset>