2016年4月21日 星期四

使用android:Theme.Holo.Dialog設定最小寬度

目的

使用 DialogFragment 添加 AlertDialog,並自訂內容畫面設計,直接設定最小畫面寬度

方法

PhotoDialogFragment.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class PhotoListDialogFragment extends DialogFragment {

    private String img;

    public PhotoListDialogFragment(String img) {
        this.img = img;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction

        View content = LayoutInflater.from(getContext()).inflate(R.layout.photo_list_dialog_content, null, false);
        TextView iv = (TextView) content.findViewById(R.id.photoListDialogIV);
        iv.setText(img);

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.DialogStyle);
        builder.setView(content)
                .setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // FIRE ZE MISSILES!
                    }
                });
        // Create the AlertDialog object and return it

        return builder.create();
    }
}

style.xml

1
2
3
4
5
6
<resources>
    <style name="DialogStyle" parent="android:Theme.Holo.Dialog">
        <item name="android:windowMinWidthMajor">90%</item>
        <item name="android:windowMinWidthMinor">90%</item>
    </style>
</resources>




reference:
http://stackoverflow.com/a/28519059

沒有留言:

張貼留言