Android 弹出对话框屏幕大小在 android Oreo 中没有改变
Android popup dialog screen size doesn't change in android Oreo
我正在处理 android 项目。我想在我的 activity 中显示一个弹出对话框。我已经实现了它(下面附有所有代码)。但我的问题是,当我 运行 这个项目在 android 中时,弹出窗口显示如下。 我需要增加这个弹出对话框的宽度。我尝试了一些解决方案,但对我没有用。 请帮我解决这个问题。提前致谢。
这是我的代码
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="wrap_content"
android:id="@+id/lv_main">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="22sp"
android:text="MSGP3006"
android:textColor="@color/Black"
android:textStyle="bold"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="QTY:2 | Weight : 500g | Discount::25%"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_marginBottom="@dimen/dimen_5dp"
android:layout_gravity="bottom">
<TextView
android:id="@+id/amount"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Rs.360.00"
android:gravity="end"
android:textColor="@color/Sky_Dark_Blue"
android:textSize="22sp"/>
</LinearLayout>
</LinearLayout>
java 文件--
public class AddInvoiceDialog extends Dialog {
public Activity activity;
public Dialog dialog;
public Button btn_add;
public AddInvoiceDialog(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.activity = a;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.inv_add_new_invoice_popup);
btn_add = (Button) findViewById(R.id.btn_add);
btn_add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
activity.finish();
}
});
}
}
在setContentView(R.layout.inv_add_new_invoice_popup);
后添加下行
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
您将获得 window 匹配设备宽度的弹出窗口。
您还需要在您的父项LinearLayout
中添加android:orientation="vertical"
,它包含多个子项。
您可以在警报对话框中设置自定义视图,如下所述:
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
查看 dialogView = inflater.inflate(R.layout.inv_add_new_invoice_popup, null);
alertDialog.setView(对话视图);
alertDialog.setCancelable(假);
alertDialog.show();
我正在处理 android 项目。我想在我的 activity 中显示一个弹出对话框。我已经实现了它(下面附有所有代码)。但我的问题是,当我 运行 这个项目在 android 中时,弹出窗口显示如下。 我需要增加这个弹出对话框的宽度。我尝试了一些解决方案,但对我没有用。 请帮我解决这个问题。提前致谢。
这是我的代码
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="wrap_content"
android:id="@+id/lv_main">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="22sp"
android:text="MSGP3006"
android:textColor="@color/Black"
android:textStyle="bold"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="QTY:2 | Weight : 500g | Discount::25%"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_marginBottom="@dimen/dimen_5dp"
android:layout_gravity="bottom">
<TextView
android:id="@+id/amount"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Rs.360.00"
android:gravity="end"
android:textColor="@color/Sky_Dark_Blue"
android:textSize="22sp"/>
</LinearLayout>
</LinearLayout>
java 文件--
public class AddInvoiceDialog extends Dialog {
public Activity activity;
public Dialog dialog;
public Button btn_add;
public AddInvoiceDialog(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.activity = a;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.inv_add_new_invoice_popup);
btn_add = (Button) findViewById(R.id.btn_add);
btn_add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
activity.finish();
}
});
}
}
在setContentView(R.layout.inv_add_new_invoice_popup);
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
您将获得 window 匹配设备宽度的弹出窗口。
您还需要在您的父项LinearLayout
中添加android:orientation="vertical"
,它包含多个子项。
您可以在警报对话框中设置自定义视图,如下所述:
AlertDialog alertDialog = new AlertDialog.Builder(context).create(); LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 查看 dialogView = inflater.inflate(R.layout.inv_add_new_invoice_popup, null); alertDialog.setView(对话视图); alertDialog.setCancelable(假); alertDialog.show();