我正在开发一个 Android
应用程序,其中有带有自定义 layout
的 Dialogs
。起初我决定使用没有标题和按钮的 AlertDialog
,因为我在自定义 layout
中有自己的 Buttons
。这在 Android
4.4 中工作正常,但在 Android
2.3 中不正常。
这是它在 Android
4.4 中的显示方式:
这是它在 Android
2.3 中的显示方式:
请注意我的 Dialog
后面奇怪的灰色背景。
在 SO 中我看到有些人使用 Dialog
而不是 AlertDialog
解决了它。我试过了,它确实修复了它,但现在我的 Dialog
填满了整个屏幕:
我不想这样,我希望 Dialog
像 AlertDialog
一样有一定的边距。
这是我的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/dialog_background"
android:orientation="vertical" >
<TextView
android:id="@+id/adTitleTextView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:layout_marginTop="1dp"
android:background="@drawable/background_green"
android:gravity="center"
android:textColor="#FFFFFFFF"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#FFFFFFFF" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:paddingBottom="15dp"
android:paddingTop="10dp"
android:textColor="#FFFFFFFF" />
<LinearLayout
android:id="@+id/adButtonBar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
<Button
android:id="@+id/adNegativeButton"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/selector_button_alert_dialog"
android:textColor="#FFFFFFFF"
android:textSize="14sp" />
<Button
android:id="@+id/adNeutralButton"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/selector_button_alert_dialog"
android:textColor="#FFFFFFFF"
android:textSize="14sp" />
<Button
android:id="@+id/adPositiveButton"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/selector_button_alert_dialog"
android:textColor="#FFFFFFFF"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
这就是我创建 Dialog
的方式:
Dialog mDialog = new Dialog( this, android.R.style.Theme_Dialog );
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
View content = getLayoutInflater().inflate( R.layout.alert_dialog_layout, null);
((TextView)content.findViewById(R.id.adTitleTextView)).setText( title );
((TextView) content.findViewById( R.id.adBodyTextView )).setText( message );
mDialog.setContentView( content );
Button b = (Button) content.findViewById(R.id.adPositiveButton);
b.setText( posText );
b.setOnClickListener( this );
b = (Button) content.findViewById(R.id.adNegaveButton);
b.setText( negText );
b.setOnClickListener( this );
mDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
mDialog.show();
这就是我创建 AlertDialog
的方式:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder( ctx );
alertDialogBuilder.setView( content );
final AlertDialog mDialog = alertDialogBuilder.create();
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
...
任何人都知道如何从 Android
2.3 中的 AlertDialog
中删除那个奇怪的背景,或者如何使 Dialog
不填满整个屏幕?
谢谢!
最佳答案
虽然我不太喜欢,但我终于找到了解决方案。我正在解决 Dialog
不是全屏问题。
基本上,我以编程方式设置宽度,如果高度超过某个值,我也设置它:
content.getLayoutParams().width = (int) (screenWidth * 0.95);
content.post(new Runnable() {
@Override
public void run() {
int newMaxHeight = content.findViewById(R.id.adTitleTextView).getHeight() +
content.findViewById(R.id.adBodyContainer).getHeight() +
content.findViewById(R.id.adButtonBar).getHeight();
if( newMaxHeight > screenHeight * 0.92 ){
content.getLayoutParams().height = (int) (screenHeight * 0.92);
content.findViewById(R.id.adBodyContainer).getLayoutParams().height =
(int) (screenHeight * 0.92) - content.findViewById(R.id.adTitleTextView).getHeight()
- content.findViewById(R.id.adButtonBar).getHeight();
}
}
});
通过这段代码,我可以限制对话框的最大宽度和高度。如果有人找到更好的解决方案,请在此处发布。
关于Android:在 Android 2.3 中,对话框不是全屏或没有奇怪背景的 AlertDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22091347/