简体   繁体   English

如何在 react-native 中设置 Alert 元素的样式?

[英]how do I style an Alert element in react-native?

I'm working with react-native and I have created an Alert element.我正在使用 react-native 并创建了一个 Alert 元素。

Alert.alert(
    'Warning',
    'bla bla bla',
    [
           {text: 'OK', onPress: () => console.log('OK Pressed')},
    ]
)

Now I'd like to apply some style to it.现在我想对它应用一些样式。 Let's say I want to apply to it a red background color and white color for the text.假设我想为文本应用红色背景颜色和白色。

How can I achieve this?我怎样才能做到这一点? Is it possible?是否可以?

There actually is a way to customize the text on the buttons but you're limited to what is provided... Below is the type definition from react native.实际上有一种方法可以自定义按钮上的文本,但您仅限于提供的内容……以下是来自 react native 的类型定义。 Then a simple example showing a red button.然后是一个显示红色按钮的简单示例。 Basically the "default" is blue, "cancel" is bold but still blue and "destructive" is red.基本上“默认”是蓝色,“取消”是粗体但仍然是蓝色,“破坏性”是红色。

**
 * An Alert button style
*/
export type AlertButtonStyle = $Enum<{
/**
* Default button style
*/
'default': string,
/**
* Cancel button style
*/
'cancel': string,
/**
* Destructive button style
*/
'destructive': string,
}>;


Alert.alert("Look out!",
        "Hitting reset will wipe all the app's data on your phone. This cannot be undone!",
        [
        {text: 'Reset', onPress: this._doSomethingSerious, style: 'destructive'},
        {text: 'Cancel'},
        ],
        {cancelable: false}
    )

You can use a custom library like react-native-modalbox .您可以使用像react-native-modalbox这样的自定义库。

You'll be able to style the modal as you like:您将能够根据需要设置模态样式:

        <Modal style={{background: 'red'}} ref={"modal1"}>
          <Text style={{color: 'white'}}>Basic modal</Text>
          <Button onPress={this.toggleSwipeToClose} style={styles.btn}>Disable swipeToClose({this.state.swipeToClose ? "true" : "false"})</Button>
        </Modal>

You can open the modal using您可以使用打开模态

openModal1(id) {
    this.refs.modal1.open();
  }

Check out the example to see more options.查看示例以查看更多选项。

Bonus : If you want to find a good library for react-native, try js.coach奖励:如果您想为 react-native 找到一个好的库,请尝试js.coach

I don't think this is possible, because the Alert component of react-native is something included in Android and iOS and cannot be modified :/我认为这是不可能的,因为 react-native 的Alert组件包含在 Android 和 iOS 中,无法修改:/

I recommend this kind of similar probleme here !在这里推荐这种类似的问题!

See ya !拜拜 !

I've been looking for this in android/rn sources.我一直在 android/rn 资源中寻找这个。

Looking at the implementation code, it instantiates an AlertDialog.Builder , and looking at the android original source code for the AlertDialog.Builder , the constructor with only context uses a resolveDialogTheme with themeResId=0 .纵观执行代码,它实例化一个AlertDialog.Builder ,看着Android的原始源代码AlertDialog.Builder ,只有上下文的构造采用了resolveDialogThemethemeResId=0 Anyway, the important part about this method is this:无论如何,关于这个方法的重要部分是这样的:

} else {
   final TypedValue outValue = new TypedValue();
   context.getTheme().resolveAttribute(R.attr.alertDialogTheme, outValue, true);
   return outValue.resourceId;
}

The fallback style/theme is alertDialogTheme , so you can override the alert dialog default theme with android:alertDialogTheme like this:后备样式/主题是alertDialogTheme ,因此您可以使用android:alertDialogTheme覆盖警报对话框默认主题,如下所示:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:alertDialogTheme">@style/ThemeAlert</item>
    </style>

    <style name="ThemeAlert" parent="Theme.AppCompat.Light.Dialog.Alert">
        <!-- This one changes buttons text color -->
        <item name="colorAccent">#152849</item>
    </style>
</resources>

I needed to change the text for default buttons, so this worked for me (at least on RN 0.61.5), but I'm not sure about which props could be changed.我需要更改默认按钮的文本,所以这对我有用(至少在 RN 0.61.5 上),但我不确定可以更改哪些道具。

Of course, this doesn't solve changing colors at runtime... if that's really needed you probably should use a lib or write a native module yourself.当然,这并不能解决运行时更改颜色的问题……如果确实需要这样做,您可能应该使用 lib 或自己编写本机模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM