将 Web 的响应式设计引入 React Native
All issues that are not bugs are appropriately labeled and closed.
For now we have three kinds of non-bug issues that are open:
In your project folder, yarn add react-native-responsive-grid
*For best results, use React Native 0.50 or later
This grid is based on row and column components that can be nested in an alternating pattern to build a responsive and adaptive 'tree of Views' of any shape and depth. It eschews complicated Flexbox-based size constraints in favor of simple percentage-based size constraints. And it does so while using Flexbox-based vertical and horizontal alignment constraints.
A Column may contain one or more Rows, each of which may contain one or more Columns, each of which may contain one or more Rows, and so on. Both Rows and Columns can be styled using predictable, percentage-based dimensions and, in case of Columns, percentage-based horizontal offset.
Rows can be aligned inside Columns, vertically (along main axis,) and aligned and stretched horizontally (along cross axis.) Columns can be aligned inside Rows, horizontally (along main axis), and and aligned and stretched vertically (along cross axis.) Additionally, the lines created within a Row that wraps may be aligned and stretched vertically relative to a parent Column.
With these basic features, we can build the entire UI component tree (or an individual component's subtree) as a consistent, repeatable and recursive pattern, one that has predictable and dynamic --not only static-- responsiveness and the ability to trigger specific adaptive behavior.
When To Use Flexbox Sizing:
In some cases when having an absolutely sized view followed (vertically or horizontally) by a view that must take up the remaining space, we'll need to use a wrapping grid element -- Column (to wrap vertical layout) or Row (to wrap horizontal layout) -- with style={{flex: 1}} and same on the Flex sized element that it wraps along with the absolutely sized element. However, such mixing of absolute and Flex sizing is not recommended as it won't lead to a fully responsive UI layout.
The only other reason to use Flexbox grow/shrink sizing with this Grid is for grow-and-shrink-in-place UI (aka "squishy" UI) where elements shrink and grow in elastic fashion and relative to each other instead of undergoing dynamic layout change and/or staying in proportion to screen width.
You may use this grid to build responsive 2D layouts that maintain their relative proportions, change their basic structure in a predictable way and dynamically decide what content to display, based on screen size, aspect ratio, and orientation.
[ section.) It also demonstrates who to wrap Row content (e.g. child columns) based on screen size (also see Example 4)
…Row: Flexbox View with flexDirection set to 'row' with convenient props and dynamic behavior.
Col: Flexbox View with flexDirection set to 'column' with convenient props and dynamic behavior.
Grid: an optional, stateful, component with style={{flex: 1}}. The Grid uses the children-as-funnction pattern and passes its state to its children, and allows state to be declared in its props, which will have the latest screen and grid info after orientation changes. It also passes it's render-triggering async setState method to its children.
Important:
Grid component is required if you need to re-run the render() function in response to orientation change (many examples here)
Grid component is also required if you use aspectRatio prop on Rows or Columns since the selection of content of the closest aspect ratio requires re-running the render function after orientation change.
Below is an example:
export const Home = () => (
{({state, setState}) => (
{/* possibly other JSX here */}
{layout(state)}
)}
)import { Row, Column as Col, ScreenInfo, Grid} from './grid'
ScreenInfo() This will return the following data:
{
mediaSize: mediaSizeWidth,
mediaSizeWidth,
mediaSizeHeight,
width: SCREEN_WIDTH,
height: SCREEN_HEIGHT,
aspectRatio: {currentNearestRatio, currentOrientation}
}sm, md, lg, xl screen width categories and is aliased to mediaSizeWidthhidden prop based on screen height category and for Row size props.if sizeOnly is true it will drop aspectRatio and its 'nearest match' calculation (shaves a few ms)
Row and Column both have .hide() and .show() instance methods. The instance reference you get from a ref callback will have these methods. See Example #1 for usage.
These are provided mainly for unit tests, except for componentInstance.hidden and componentInstance.shown which can be used to tell the state of the component.
All props are case sensitive.
aspectRatio (see Example 3)
size may be supplied as prop to Column (width) or Row (height). This number defines the width of the column or height of a row as a percentage of its parent view's computed or explicit width or height, respectively.
smSize, mdSize, lgSize and xlSize are device-dependent size values that are applied to Columns (which map to width percent) and Rows (which map to height percent.) In addition to their utility in deciding the size of content based on screen size (width in case of Columns and height in case of Rows), they may are also used for defining column wrapping behavior based on screen size. For example, Columns in as Row will wrap if Row width becomes smaller at smaller screen sizes.
sizePoints may be supplied as prop to Column (which map to width points) or Row (which map to height points). This number defines the width of the column or height of a row as an asolute value in points.
smSizePoints, mdSizePoints, lgSizePoints, and xlSizePoints are like their percentage-based equivalents but use point values.
offset may be applied to Column. This number defines the marginLeft (or marginRight in csase of RTL mode) for the column as a percentage of its parent view's computed or explicitly set width. Offset values can also be negative. Default is 0.
smOffset, mdOffset, lgOffset and xlOffset are device-dependent offset values that are applied to columns.
offsetPoints, mdOffsetPoints, lgOffsetPoints, and xlOffsetPoints are like their percentage-based equivalents (i.e. applied to Column to produce offset) but use value in points instead of value in percent.
Using offset values in RTL mode moves things from right to left. Using them in normal LTR mode moves things from left to right. It's pretty normal to expect that. If you're working in both directions, this makes offsets more useful than using marginLeft or marginRight directly.
Specifying an offset value in normal LTR mode means marginLeft (if specified in style prop) will be overwritten by offset value. However, marginRight (if specified in style prop) will not be overwritten by the offset value. Specifying offset value in RTL mode means marginRight (if specified in style prop) will be overwritten by offset value. However, marginLeft (if specified in style prop) will not be overwritten by offset value.
smHidden, mdHidden, lgHidden and xlHidden - may be applied to Column or Row which tells the parent Row or Column, respectively, to hide the affected child Column or child Row based on the current width (for child Columns) or height (for child Rows) of the screen.
The screen-size-specific size and hidden props refer to the current screen width in case of Columns and current screen height in case of Rows, which changes with orientation. The offset props only apply to Columns so they refer to the current screen width.
The following are the device width (for Columns) and height (for Rows) thresholds for these props:
The preset values may be overridden with setBreakPoints which merges the parameter object with the defaults. Each cutoff specifies the upper end for that range. XLARGE_Width is inferred from anything above LARGE_Width. BreakPoints should be set early in the app such as in index.js. An example overriding the SMALL_Width, MEDIUM_Width, and LARGE_Width break points:
import { setBreakPoints } from 'react-native-responsive-grid';
setBreakPoints({
SMALL_Width: 414,
MEDIUM_Width: 600,
LARGE_Width: 1024
})vAlign may be supplied as prop to Column to vertically align the elements and/or rows within it. Possible values are: middle | center, top, bottom, space and distribute. Default is top.
vAlign may also be supplied as prop to Row to align the columns within it in the vertical direction. Possible values are: top, middle | center, bottom, baseline and stretch. Default is stretch.
hAlign may be supplied as prop to Row to align its child Columns and/or elements within it in the horizontal direction. Possible values are: center | middle, left, right, space and distribute. Default is left.
hAlign may also be supplied as prop to Column to align its child Rows and/or elements within it in the horizontal direction. Possible values are: center | middle, left, right, and stretch. Default is stretch.
rtl may be supplied as prop to Row to both reverse the order of columns (or elements) inside a row as well as to set hAlign to 'right.' This is useful for right-to-left layouts.
fullHeight may be supplied as prop to Row in place of size={100} or style={{height: '100%'}} -- note that Rows have 0 height and width by default, but a fullHeight Row inside of a fullWidth Column will have height and width of 100%
fullWidth may be supplied as prop to Column in place of size={100} or style={{width: '100%'}} -- note that Columns have 0 height and width by default, but a fullWidth Column inside of a fullHeight Row will have height and width of 100%
alignLines may be supplied as prop to Row to vertically align the wrapped lines within the Row (not to be confused with the items that are inside each line.) Possible values are: top, middle, bottom, space, distribute, stretch. (See section on Aligning Wrapped Lines within Rows)
alignSelf maybe supplied as prop to Row to override the hAlign prop of the parent Column for that Row.
Possible values are: auto, left, right, center | middle, stretch
alignSelf maybe supplied as prop to Column to override the vAlign prop of the parent Row for that Column.
Possible values are: auto, top, bottom, middle | center, stretch, baseline
noWrap may be supplied as prop to Row prevent child elements from wrapping.
If you're nesting a column inside a row which is inside another column that is inside another row as below:
This column is 25% of the outer view's width (or 25% of the screen width if
the top level Row has no parent)
The nested column's size will be the column size value (size, sm, md, lg, xl) as a percentage of
暂无开放 Issues,或尚未同步最近议题。