95

导入_this

import {AppRegistry, Text, View, Button, StyleSheet} from 'react-native';

这是我的 React Button 代码但样式不起作用 Hare ...

<Button
  onPress={this.onPress.bind(this)} 
  title={"Go Back"}
  style={{color: 'red', marginTop: 10, padding: 10}}
/>

我也尝试过这段代码

<Button 
       containerStyle={{padding:10, height:45, overflow:'hidden', 
       borderRadius:4, backgroundColor: 'white'}}
       style={{fontSize: 20, color: 'green'}} 
       onPress={this.onPress.bind(this)} title={"Go Back"}
      > Press me!
</Button>

更新问题:

我也尝试过这种方式..

<Button
    onPress={this.onPress.bind(this)}
    title={"Go Back"}
    style={styles.buttonStyle}
>ku ka</Button>

风格

const styles = StyleSheet.create({
    buttonStyle: {
        color: 'red',
        marginTop: 20,
        padding: 20,
        backgroundColor: 'green'
    }
});

但没有输出:我的手机截图:- 我的手机截图:-

4

14 回答 14

131

React Native Button的功能非常有限,请参阅;按钮

它没有style prop,并且您不会像<Button>txt</Button>通过 title 属性那样设置“web-way”文本<Button title="txt" />

如果您想更好地控制外观,您应该使用 TouchableXXXX 的组件之一,例如TouchableOpacity 它们真的很容易使用 :-)

于 2017-07-06T09:32:20.113 回答
46

我遇到了边距和填充的问题Button。我在组件中添加了 ButtonView并将您的属性应用于View.

<View style={{margin:10}}>
<Button
  title="Decrypt Data"
  color="orange"
  accessibilityLabel="Tap to Decrypt Data"
  onPress={() => {
    Alert.alert('You tapped the Decrypt button!');
  }}
  />  
</View>
于 2018-02-04T04:29:44.667 回答
16

React Native 按钮在它们提供的选项中非常有限。您可以使用 TouchableHighlight 或 TouchableOpacity 通过设置这些元素的样式并像这样用它包裹您的按钮

             <TouchableHighlight 
                style ={{
                    height: 40,
                    width:160,
                    borderRadius:10,
                    backgroundColor : "yellow",
                    marginLeft :50,
                    marginRight:50,
                    marginTop :20
                }}>
            <Button onPress={this._onPressButton}            
            title="SAVE"
            accessibilityLabel="Learn more about this button"
          /> 
          </TouchableHighlight> 

您还可以将反应库用于自定义按钮。一个不错的库是 react-native-button ( https://www.npmjs.com/package/react-native-button )

于 2018-05-15T05:00:35.633 回答
12

如果您不想创建自己的按钮组件,一个快速而肮脏的解决方案是将按钮包装在视图中,这样您至少可以应用布局样式。

例如,这将创建一行按钮:

<View style={{flexDirection: 'row'}}>
    <View style={{flex:1 , marginRight:10}} >
        <Button title="Save" onPress={() => {}}></Button>
    </View>
    <View style={{flex:1}} >
        <Button title="Cancel" onPress={() => {}}></Button>
    </View>
</View>
于 2017-12-31T12:56:42.157 回答
10

而不是使用按钮。您可以在本机反应中使用文本,然后使其可触摸

<TouchableOpacity onPress={this._onPressButton}> 
   <Text style = {'your custome style'}>
       button name
   </Text>
</TouchableOpacity >
于 2018-10-09T06:37:53.140 回答
6

按钮中的样式不起作用,您必须为视图赋予样式。

<View style={styles.styleLoginBtn}>
          <Button
            color="orange" //button color
            onPress={this.onPressButton}
            title="Login"
          />
        </View>

将此样式提供给查看

const styles = StyleSheet.create({
  styleLoginBtn: {
    marginTop: 30,
    marginLeft: 50,
    marginRight: 50,
    borderWidth: 2,
    borderRadius: 20,
    borderColor: "black", //button background/border color
    overflow: "hidden",
    marginBottom: 10,
  },
});
于 2021-09-29T10:45:17.303 回答
3

只有自己学习,但包装在视图中可能允许您在按钮周围添加样式。

const Stack = StackNavigator({
  Home: {
    screen: HomeView,
    navigationOptions: {
      title: 'Home View'
    }
  },
  CoolView: {
    screen: CoolView,
    navigationOptions: ({navigation}) => ({
      title: 'Cool View',
      headerRight: (<View style={{marginRight: 16}}><Button
        title="Cool"
        onPress={() => alert('cool')}
      /></View>
    )
    })
  }
})
于 2017-11-17T19:27:36.650 回答
2

试试这个

<TouchableOpacity onPress={() => this._onPressAppoimentButton()} style={styles.Btn}>
    <Button title="Order Online" style={styles.Btn} > </Button>
</TouchableOpacity>
于 2018-07-24T10:56:45.630 回答
1

正如@plaul 的回答所提到的TouchableOpacity,这是一个如何使用它的示例;

  <TouchableOpacity
    style={someStyles}
    onPress={doSomething}
  >
    <Text>Press Here</Text>
  </TouchableOpacity>

建议:

我会推荐使用react-native-paper组件,因为它们是经过修改的,并且可以比react-native组件进行更多的修改。

安装; npm install react-native-paper

然后您可以简单地导入它们并使用。

更多细节在这里

于 2021-01-22T06:23:12.457 回答
1

按钮样式在 react-native 中不起作用,以 react-native 简单的方式设置按钮样式是将其放在View块中,如下所示:

      <View
         style={styles.buttonStyle}>
         <Button
         title={"Sign Up"}
         color={"#F31801"}/>
      </View>

style.buttonStyle是这样的:

style.buttonStyle{
        marginTop:30,
        marginLeft:50,
        marginRight:50,
        borderWidth:2,
        borderRadius:20,
        borderColor:'#F31801',
        overflow:"hidden",
        marginBottom:10,
}

,它将使您能够使用带有按钮的设计

于 2021-08-18T07:54:58.673 回答
1

我们现在可以使用buttonStyle道具了。
https://react-native-training.github.io/react-native-elements/docs/button.html#buttonstyle

于 2019-04-23T16:00:55.690 回答
1

您可以使用PressablewithText代替按钮。

import { StyleSheet, Text, View, Pressable } from 'react-native';

<Pressable style={styles.button} onPress = {() => console.log("button pressed")}> 
  <Text style={styles.text}>Press me</Text>
</Pressable>

示例样式:

const styles = StyleSheet.create({
  button: {
    alignItems: 'center',
    justifyContent: 'center',
    paddingVertical: 12,
    paddingHorizontal: 32,
    borderRadius: 4,
    elevation: 3,
    backgroundColor: 'red'
  },
  text: {
    fontSize: 16,
    lineHeight: 21,
    fontWeight: 'bold',
    letterSpacing: 0.25,
    color: 'white',
  },
});
于 2021-11-03T08:21:14.360 回答
0

React-native 按钮非常有限,它不允许样式。使用反应原生元素按钮或创建自定义按钮

于 2021-09-09T08:45:14.660 回答
-2

我知道这是 necro-posting,但我找到了一种非常简单的方法,只需将 margin-top 和 margin-bottom 添加到按钮本身,而无需构建其他任何东西。

当您创建样式时,无论是内联还是通过创建要传递的对象,您都可以这样做:

var buttonStyle = {
   marginTop: "1px",
   marginBottom: "1px"
}

似乎在值周围添加引号使其起作用。我不知道这是不是因为它是 React 的更高版本,而不是两年前发布的版本,但我知道它现在可以工作。

于 2019-10-08T14:09:47.420 回答