使用API​​10更改DialogFragment中主题的颜色

时间:2013-12-09 21:35:36

标签: android styles android-dialogfragment

我有一个适用于api 11+的dialogFragment,它出现在HOLO_LIGHT主题中。 对于API 10来说,对话碎片是黑暗的,我希望它很轻,这样我的黑色图标就能显示出来。

我不能使用HOLO_LIGHT,因为它需要API 11+ 我试图改变它,但它永远不想工作。

这是最小的代码:

MainActivity:

public class MainActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button b1 = (Button)findViewById(R.id.button1);
    b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            FragmentManager fm = getSupportFragmentManager();
            AddCommentDialog testDialog = new AddCommentDialog();
            testDialog.setStyle(DialogFragment.STYLE_NO_TITLE, R.style.MyFragment);
            testDialog.show(fm, "fragment_name");       
        }
    });
}
}

AddCommentDialog:

public class AddCommentDialog extends DialogFragment {
// private final String TAG = getClass().getSimpleName();

private Context context;

public AddCommentDialog() {
    // Empty constructor required for DialogFragment
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContext(getActivity());

}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    getDialog().getWindow().setSoftInputMode(
            LayoutParams.SOFT_INPUT_STATE_VISIBLE);

    super.onActivityCreated(savedInstanceState);
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // setStyle(DialogFragment.STYLE_NORMAL, 0);

    AlertDialog.Builder principal = new AlertDialog.Builder(getActivity());

    principal.setTitle(R.string.report);
    View view = getActivity().getLayoutInflater().inflate(
            R.layout.add_comment, null);

    principal.setView(view);

    AlertDialog ad = principal.create();
    return ad;

}

public Context getContext() {
    return context;
}

public void setContext(Context context) {
    this.context = context;
}

}

和styles.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android">

<style name="AppBaseTheme" parent="android:Theme.Light"></style>

<style name="AppTheme" parent="AppBaseTheme"></style>

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

</resources>

我不发布add_comment.xml,因为dialogFragment的颜色与他的内容无关。

0 个答案:

没有答案