As you see the image, if the three red views are already added on the parent view. Now I want to add another blue view which can fill the rest space. How can I set the style?
You can try this;
<View style={{flex:1,backgroundColor:'white'}}>
<View style={{justifyContent:'space-around'}}>
<View style={{height:50,alignSelf:'stretch',backgroundColor:'pink',margin:5}}/>
<View style={{height:50,alignSelf:'stretch',backgroundColor:'pink',marginHorizontal:5}}/>
<View style={{height:50,alignSelf:'stretch',backgroundColor:'pink',margin:5}}/>
</View>
<View style={{flex:1,alignItems:'center',justifyContent:'center',alignSelf:'stretch',backgroundColor:'blue',margin:5}}>
<Text style={{color:'white',fontWeight:'bold'}}>
View
</Text>
</View>
</View>
A much easier solution is to use the attribute flexGrow: 1 on the View you want to fill the remaining space.
flexGrow describes how any space within a container should be distributed among its children along the main axis. After laying out its children, a container will distribute any remaining space according to the flex grow values specified by its children.
flexGrow accepts any floating point value >= 0, with 0 being the default value. A container will distribute any remaining space among its children weighted by the child’s flex grow value.
https://facebook.github.io/react-native/docs/flexbox
DEMO
Code
import * as React from 'react';
import { Text, View, StyleSheet } from 'react-native';
export default class App extends React.Component {
render() {
return (
<View style={styles.container}>
<View style={styles.flexContainer}>
<View style={styles.box1}></View>
<View style={styles.box2}></View>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
padding: 8,
},
flexContainer: {
backgroundColor: 'black',
height: 100,
flexDirection: 'row'
},
box1: {
backgroundColor: 'blue',
width: 100,
height: '100%'
},
box2: {
backgroundColor: 'red',
height: '100%',
flexGrow: 1
}
});
Related
I'm creating a react native app and I need to create a curved screen design. How to create a curve design like the below screen.
This is what I tried here,
My sample codes,
import React from 'react';
import {
StyleSheet,
Text,
View,
StatusBar,
} from 'react-native';
import {
widthPercentageToDP as wp,
heightPercentageToDP as hp,
} from 'react-native-responsive-screen';
export default class Home extends React.Component {
render() {
return (
<View style={styles.container}>
<StatusBar hidden={true} />
<View style={styles.container1}>
<Text style={styles.name}>Container1</Text>
</View>
<View style={styles.container2}>
<Text style={styles.name}>Container1</Text>
</View>
<View style={styles.container3}>
<Text style={styles.name}>Container1</Text>
</View>
<View style={styles.container4}>
<Text style={styles.name}>Container1</Text>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
},
container1: {
backgroundColor: '#3d06d6',
height: hp('25%'),
padding: 10,
borderBottomLeftRadius:100,
},
container2: {
backgroundColor: '#0cad95',
height: hp('25%'),
padding: 10,
borderBottomLeftRadius:100,
},
container3: {
backgroundColor: '#d6a606',
height: hp('25%'),
padding: 10,
borderBottomLeftRadius:100,
},
container4: {
backgroundColor: '#06bad6',
height: hp('25%'),
padding: 10,
borderBottomLeftRadius:100,
},
});
I want to curve my container edges like the first picture. I am able to make curves of the bottom edges. And How can I make curve designs like the first image? and also how can I add some animations into the container when I touch the container to open another screen? Is there any way to do this design using any style with "borderRadius" or any other way? And I don't want to use any library like react-native-svg.
If you need a Linear Gradient as the background. Just giving the color of the next box will not be the best option.
Assume "value" as the border-radius. ie the curve
Give a marginTop and paddingTop for each container.
marginTop : -value,
paddingTop : value,
Also assign zIndex in decending order for each container.
You can set zIndex with respect to the index, if you are looping through an array.
Result
Sample code
import React from 'react';
import {View, StyleSheet, Text} from 'react-native';
import LinearGradient from 'react-native-linear-gradient';
const data = ['', '', ''];
const App = () => {
return (
<View style={styles.container}>
{data.map((item, index) => (
<View style={[styles.item, {zIndex: data.length - index}]}> // setting zIndex here
<LinearGradient
style={StyleSheet.absoluteFill}
colors={['#4c669f', '#3b5998', '#192f6a']}
/>
</View>
))}
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
zIndex: 1,
},
item: {
height: 250,
borderBottomLeftRadius: 100, // logic goes here
marginTop: -100, // move container
paddingTop: 100, // move inner item down
overflow: 'hidden',
},
});
export default App;
You can simply wrap your view inside a normal view and set the background colour of the next box which will give the output you need.
<View style={{ backgroundColor: '#0cad95' }}>
<View style={styles.container1}>
<Text style={styles.name}>Container1</Text>
</View>
</View>
<View style={{ backgroundColor: '#d6a606' }}>
<View style={styles.container2}>
<Text style={styles.name}>Container1</Text>
</View>
</View>
I'm brand new to react native. I'm trying to figure out how to make a box full height.
I have tried to add flex property to both container and view, but the height is not 100%.
export default class LinksScreen extends React.Component {
render() {
return (
<ScrollView style={styles.container}>
<View style={{ flex: 1, backgroundColor: '#fff', flexGrow: 1 }}>
<Text>Hallo</Text>
</View>
</ScrollView>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
padding: 20,
backgroundColor: 'blue',
},
});
You need to wrap the ScrollView with a View that has height. ScrollViews must have a bounded height in order to work. You can read more details about that here: https://facebook.github.io/react-native/docs/scrollview
Do it something like below:
render() {
return (
<View style={styles.container}>
<View style={{height: 80}} >
<ScrollView
...
EDIT
Your code actually works. Maybe you just want to see if your scrollview works when the whole screen is occupied. Try this below and you will see, I simply multiplied the text contents 80 times:
render() {
return (
<ScrollView style={styles.container}>
<View style={{ flex: 1, backgroundColor: "#fff", flexGrow: 1 }}>
{Array(80)
.fill(1)
.map((item, index) => (
<Text>Hallo {index}</Text>
))}
</View>
</ScrollView>
);
}
Remove flex:1
Give height:"100%"
width: "100%"
I am studying React Native and trying to understand the behavior of the components' style.
I am just creating a simple app and testing the padding style.
Why the boxes disapear when I remove the padding style?
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
export default class App extends React.Component {
render() {
return (
<View style={styles.container}>
<View style={styles.box} />
<View style={styles.box} />
<View style={styles.box} />
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
box: {
padding:20, //remove this line
margin: 4,
backgroundColor: 'steelblue'
}
});
Cause <View /> is like <div> in HTML, it's size is determined by its content, if you remove the padding, then it occupies 0 space.
One way to keep <View /> visible even if it has no content, is to set flex in style like { flex: 1 }
The Box View needs to be either of Fixed dimensions like this:
box: {
//width: 50,
//height: 50,
padding:20,
margin: 4,
backgroundColor: 'steelblue'
}
By padding it is given a fixed size, but if you remove padding it will have no size.
Or you can use Flex dimensions like this:
box: {
flex:1,
padding:20,
margin: 4,
backgroundColor: 'steelblue'
}
In this one you can safely remove padding:20, line.
this is my genymotion screen !!!!
this is my code . Just react-native program And the trouble in my photo.Android is Not , IOS is great .how hack in android to make it right .
import React, { Component } from 'react';
import { View, Text, StyleSheet } from 'react-native';
export default class TestScreen extends Component {
render() {
return (
<View style={styles.container}>
<Text></Text>
<View style={styles.parent}>
<View style={styles.children}>
<Text>my content </Text>
</View>
</View>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex:1,
alignItems: 'center',
justifyContent: 'center',
},
parent: {
width:300,
height: 40,
backgroundColor: 'yellow',
alignItems:'center',
position:'absolute',
},
children:{
height:20,
justifyContent: 'center',
top:-10
}
})
How should I Fix it
Overflow is not supported on Android: https://github.com/facebook/react-native/issues/6802
You can try this library:
https://github.com/entria/react-native-view-overflow
Or you could try to do some sort of absolute positioning with your view, but it might not be perfect. The below example puts all of the views in one parent, and then offsets the text to overlap both with an absolute position.
render() {
return (
<View style={styles.container}>
<View style={{ height: 40}}/>
<View style={{ height: 40, backgroundColor: "yellow"}}/>
<View style={{ position: "absolute", top: 30}}>
<Text>my content </Text>
</View>
</View>
)
}
I'm trying to make a clock component with a face image and a needle centered on the clock in React Native.
does not allow embedding other Images.
I found a few posts for centering text over images but cannot find any way do center Image over images.
Edit:
I'm trying to find a way that avoids giving absolute position so the component can be dynamically sized.
If i understood correctly, you want to show one image over other image. With one as a Parent image and other as a child image without using absolute postion.
For this you can use ImageBackground Component provided by react-native and by setting its height and width with percentage value.
Below is the example :
Parent Image : Clock.png is an ImageBackground Component
Child Image : Needle.png is an Image Component
import React, { Component } from 'react'
import { StyleSheet, View, Text, ImageBackground, Image } from 'react-native'
export default class ImageView extends Component {
render() {
return (
<View
style={{
flex: 1
}}
>
<View
style={{
flex: 0.25,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: 'red'
}}
>
<Text style={{ color: 'white', fontSize: 26 }}>I am Header</Text>
</View>
<View
style={{
flex: 1,
alignItems: 'center',
justifyContent: 'center',
// backgroundColor: 'blue',
borderWidth: 1,
borderColor: 'red'
}}
>
<ImageBackground
source={require('#assets/Clock.png')}
style={{
height: '60%',
width: '100%'
}}
resizeMode="contain"
>
<Image
style={{
marginTop: '4.5%',
alignSelf: 'center',
height: '30%',
width: '100%'
}}
resizeMode="contain"
source={require('#assets/Needle.png')}
/>
</ImageBackground>
</View>
</View>
)
}
}
Same as you would handle this in regular old html,css.
Use position:absolute then align the items so that they match how you expect.