2017-04-24 84 views
14

Import_this

import {AppRegistry, Text, View, Button, StyleSheet} from 'react-native';陣營本地按鈕式的不行

這是我的陣營按鈕的代碼,但風格不工作野兔......

<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' 
    } 
}); 

但沒有出來放: 截圖我的電話: - Screenshot of my phone:-

+0

'按鈕'你使用自定義組件或反應按鈕組件? –

+0

也許會有反應'按鈕 –

+1

它沒有'style'屬性。請檢查一次。 –

回答

0

好像你正在使用'本地基礎「庫。代碼對我來說似乎很好。你能否也發送視圖的風格?

另外,這裏有什麼問題?您是否無法看到按鈕,或者您無法在此處調用綁定的操作?

+0

檢查我的更新問題Atitipatel –

+0

你能把你的js文件發給我嗎?我認爲您的按鈕的父視圖存在問題?我想在我的最後運行它。 – atitpatel

22

The 反應原生按鈕是非常有限的,你可以做什麼,請參閱; Button

它沒有一個風格道具,你不設置文本像<Button>txt</Button>而是通過title屬性的「網絡通」 <Button title="txt" />

如果你想擁有在外觀更多的控制你應該使用其中一個TouchableXXXX組件,例如TouchableOpacity 它們非常易於使用:-)

+4

TouchableOpacity的一個工作示例會很棒。 – mayid

0

只學習我自己,但是在View中包裝可能允許您在按鈕周圍添加樣式。

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> 
    ) 
    }) 
    } 
}) 
2

如果你不想創建自己的按鈕組件,一個快速和骯髒的解決辦法是包裝在一個視圖,它可以讓你至少應用佈局樣式的按鈕。

例如,這將創建一排按鈕:

<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> 
0

我曾與margin和padding問題與Button。我在View組件中添加了Button,並將您的屬性應用於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>