如何将AppCompat活动作为对话框?

时间:2016-03-09 05:10:35

标签: android alertdialog android-appcompat

我需要使用我的AppCompat Activity作为Dialog。为此我试过我的解决方案,在StackOverflow中回答。但没有任何效果。请回答我。我正在进行对话活动。但它的高度和高度都很窄。宽度。

我使用了以下主题:

<style name="AppDialogTheme" parent="Theme.AppCompat.Light.Dialog">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="windowNoTitle">true</item>
    <item name="android:background">@android:color/transparent</item>
</style>

enter image description here

2 个答案:

答案 0 :(得分:0)

您可以使用DialogFragment并相应地自定义布局。

public class CustomDialogFrag extends DialogFragment{
static FragmentManager fragmentManager;
    public static CustomDialogFrag showDialog(FragmentManager fm){
        CustomDialogFrag customDialogFrag=new CustomDialogFrag();
        fragmentManager=fm;
        return customDialogFrag;
    }
@Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
           AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
        View view = getActivity().getLayoutInflater().inflate(R.layout.dialogfrag_layout, null);
        alertDialogBuilder.setView(view);
        setupUI(view);
        alertDialogBuilder.setTitle("Notification Message");
        alertDialogBuilder.setIcon(R.drawable.notificationicon);
        alertDialogBuilder.setPositiveButton("Close", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        return alertDialogBuilder.create();
    }
void setupUI(View view){
        TextView textViewOne=(TextView)view.findViewById(R.id.txtEventAlias);
        TextView textViewTwo=(TextView)view.findViewById(R.id.txtTime);
        TextView textViewThree=(TextView)view.findViewById(R.id.txtLogMessage);
        textViewOne.setText("Text 1");
        textViewTwo.setText("Text 2");
        textViewThree.setText("Text 3");
    }
}


dialogfrag_layout.xml将是

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/margin_10"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/txtEventAlias"
        android:text="Sample"
        android:textColor="@android:color/darker_gray"
        android:textSize="@dimen/textSizeMedium"
        android:padding="@dimen/margin_10"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/txtTime"
        android:text="Sample"
        android:textColor="@android:color/darker_gray"
        android:textSize="@dimen/textSizeMedium"
        android:padding="@dimen/margin_10"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/txtLogMessage"
        android:text="Sample"
        android:textColor="@android:color/darker_gray"
        android:textSize="@dimen/textSizeMedium"
        android:padding="@dimen/margin_10"
        />
</LinearLayout>

Fragment

调用此对话框
DialogFragment dialogFragment=CustomDialogFrag.showDialog(getFragmentManager());
dialogFragment.show(getActivity().getFragmentManager(), "tag");

答案 1 :(得分:0)

在你的活动onCreate中添加以下内容:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_your);

    // Make the window's width full sized
    WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
    Window window = getWindow();

    layoutParams.copyFrom(window.getAttributes());
    layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
    layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;

    window.setAttributes(layoutParams);
}

经过测试和工作。如果需要,您可以将宽度和高度设置为WRAP_CONTENT