I'm developing an Android
application where I have Dialogs
with custom layout
. At first I decided to use AlertDialog
without title and without buttons, since I have my own Buttons
in my custom layout
. This works fine in Android
4.4, but not in Android
2.3.
This is how it's seen in Android
4.4:
This is how it's seen in Android
2.3:
Please notice the weird gray background behind my Dialog
.
In SO I saw that some people solved it using Dialog
instead of AlertDialog
. I tried it and it really fixed it, but now my Dialog
fills the whole screen:
And I don't want that, I want the Dialog
to have some margin like the AlertDialog
has.
This is my layout:
<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>
This is how I create my 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();
And this is how I create the AlertDialog
:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder( ctx );
alertDialogBuilder.setView( content );
final AlertDialog mDialog = alertDialogBuilder.create();
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
...
Anybody knows how can I remove that weird background from AlertDialog
in Android
2.3 OR how to make Dialog
not to fill the whole screen?
Thank you!
尝试这个 :
dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
I finally found a solution for this, although I don't like it much. I'm solving the Dialog
not full screen issue.
Basically, I set the width programmatically and, if the height exceeds a certain value, I set it too:
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();
}
}
});
With this code I can limit the maximum width and height of the Dialog. If anybody finds a better solution, please post it here.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.