2

在 Android 2.3.4 上使用 Sherlock:我想展示一个AlertDialog包含:

1)A 标题
2)A 内容
3)2 按钮
我正在使用以下

public class MyAlertDialog extends SherlockDialogFragment{

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

        AlertDialog.Builder builder = new AlertDialog.Builder(getSherlockActivity());
        builder.setMessage("Title")
               .setPositiveButton("Fire!", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // FIRE ZE MISSILES!
                   }
               })
               .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // User cancelled the dialog
                   }
               });
        // Create the AlertDialog object and return it
        return builder.create();
    }

}

在我的活动中,我正在打电话:

MyAlertDialog m = new MyAlertDialog();
m.show(getSupportFragmentManager(), "hey");

它正在显示 AlertDialog 但使用旧主题(请记住我使用的是 Android 2.3.4)

如果您愿意,这是我的整个主要活动课程:

public class MainActivity extends SherlockFragmentActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActionBar ab;
        ab = getSupportActionBar();
        ab.setTitle("Testing Sherlock"); 

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        getSupportMenuInflater().inflate(R.menu.main, menu);

        return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch(item.getItemId())
        {
        case R.id.action_one:
            MyAlertDialog m = new MyAlertDialog();
            m.show(getSupportFragmentManager(), "hey");
            break;
        }
        return super.onOptionsItemSelected(item);
    }
}

我为我的应用设置的样式是:

<resources>
<style name="AppBaseTheme" parent="@style/Theme.Sherlock.Light">
</resources>

PS:我有ActionBar显示,除了主题之外一切正常AlertDialog
我希望它看起来像这样:

在此处输入图像描述

而不是这样的:

在此处输入图像描述

4

3 回答 3

4

创建样式:

<resources>
    <style name="MyFragment">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowTitleStyle">@null</item>
        <item name="android:colorBackground">#ffffff</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:textColor">#FF0000</item>
    </style>
</resources>

在您的 SherlockFragmentActivity 中:

public class MyDialogFragment extends SherlockDialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.MyFragment);
        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();
        final View view = inflater.inflate(R.layout.custom_layout, null);
        ....
    }
}

R.style.MyFragment是样式。

R.style.custom_layout是 AlertDialog 的自定义布局。

于 2013-04-09T08:52:08.680 回答
4

这个库看起来很适合全息风格的 android 对话框 https://github.com/inmite/android-styled-dialogs

android风格的对话框

于 2013-08-12T12:59:09.593 回答
1

I have already done this, I don't remember the exact steps I took, but the general approach is:

Copy the XML alert_dialog_holo (I think that is the name) from:
android sdk folder\platforms\android-version using holo\data\res\layout
to your project.

I think you have to make some changes in order to make it work correctly (I don't know anymore what I have done exactly, but I believe to remember you must remove the "android:" prefix from some style definitions, then they will be recognized by ABS.

Maybe you also need styles generated from this tool, but I don't know if they are implied to make it work.

Also take a look at this question the corresponding answers will lead you the way how to inflate your own XML in your Dialog

于 2013-03-17T17:06:33.440 回答