简体   繁体   English

任何人都可以在用户界面风格定位方面帮助我

[英]could anyone kindly help me out with material ui style positioning

在此处输入图片说明

Above image is what I'm currently getting. 上面的图像是我当前正在得到的。 I want the icon to be placed at the right-hand end. 我希望将图标放置在右侧。 I'm using material-ui design right now. 我现在正在使用material-ui设计。 How can I do this? 我怎样才能做到这一点?

 import React from 'react';
 import { makeStyles } from '@material-ui/core/styles';
 import {
 Grid, AppBar, Toolbar, Typography
 } from '@material-ui/core';
 import ShoppingCartIcon from '@material-ui/icons/ShoppingCart';
 import Badge from '@material-ui/core/Badge';
 import IconButton from '@material-ui/core/IconButton';

class Appbar extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
   }
  }

  render() {
    const classes = makeStyles(theme => ({
      root: {
        flexGrow: 1,
      },
      title: {
        marginRight: theme.spacing(2),
      },
      cart: {
        flexGrow:1,
      },
   }));
    return (
      <div className={classes.root}>
       <AppBar position="static">
          <Typography className={classes.title} variant="h6">
            THIS IS IT
          </Typography>
            <IconButton className ={classes.cart} aria- 
  label="Cart">
          <Badge badgeContent={100} color="primary">
            <ShoppingCartIcon/>
          </Badge>
        </IconButton>
    </AppBar>
  </div>
    );
  }
}

export default Appbar;

演示应用栏

You can see it working here : 您可以在这里看到它的工作方式:

Here is the code snippet: 这是代码片段:

import React from 'react';
import AppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar';
import MenuIcon from '@material-ui/icons/Menu';
import IconButton from '@material-ui/core/IconButton';
import Typography from '@material-ui/core/Typography';
import Button from '@material-ui/core/Button';
import ShoppingCartIcon from '@material-ui/icons/ShoppingCart';
import { withStyles } from '@material-ui/core/styles';

const styles = theme => ({
  root: {
    flexGrow: 1,
  },
  grow: {
    flexGrow: 1,
  },
  menuButton: {
    marginLeft: -12,
    marginRight: 20,
  },
  button: {
    marginRight: 6,
  },
  rightIcon: {
    marginLeft: theme.spacing.unit,
  },
});

class Appbar extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
   }
  }

  render() {
    const { classes } = this.props;

    return (
      <div className={classes.root}>
      <AppBar position="static">
        <Toolbar>
          <IconButton
            className={classes.menuButton}
            color="inherit"
            aria-label="Menu"
          >
            <MenuIcon />
          </IconButton>
          <Typography variant="h6" color="inherit" className={classes.grow}>
            Demo
          </Typography>
          <Button
            color="inherit"
            className={classes.button}
          >
            <ShoppingCartIcon className={classes.rightIcon} />
          </Button>
        </Toolbar>
      </AppBar>
    </div>
    );
  }
}

export default withStyles(styles)(Appbar);

I gave flexGrow property to the title to let it grow, remaining space in the container is distributed equally to all children. 我给flexGrow属性赋予标题以使其增长,容器中的剩余空间平均分配给所有子级。 You can learn more about it here . 您可以在此处了解更多信息。

This is more of a CSS question than a Material-UI question. 与Material-UI问题相比,这更多是CSS问题。

You should give the parent where the icon and font are contained the following 2 rules in css: 您应该在css中给父图标和字体包含以下2条规则的位置:

display: flex,
justify-content: space-between

If you want more information about flexbox, go to this link 如果您想了解有关flexbox的更多信息,请转到此链接

I would add a class to the appbar that gives it row flex direction and add a div to help with the spacing. 我会在appbar中添加一个类,为它提供行弹性方向,并添加一个div以帮助间距。 The final solution should look something like this: 最终解决方案应如下所示:

 import React from 'react';
 import { makeStyles } from '@material-ui/core/styles';
 import {
 Grid, AppBar, Toolbar, Typography
 } from '@material-ui/core';
 import ShoppingCartIcon from '@material-ui/icons/ShoppingCart';
 import Badge from '@material-ui/core/Badge';
 import IconButton from '@material-ui/core/IconButton';

class Appbar extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
   }
  }

  render() {
    const classes = makeStyles(theme => ({
      root: {
        flexGrow: 1,
      },
      title: {
        marginRight: theme.spacing(2),
      },
      appBar: {
        display: 'flex',
      }
      spacer: {
        flexGrow: 1,
      },
      cart: {
        flexGrow:0,
      },
   }));
    return (
      <div className={classes.root}>
       <AppBar position="static" className={classes.appBar}>
          <Typography className={classes.title} variant="h6">
            THIS IS IT
          </Typography>
          <div className={classes.spacer}/>
          <IconButton className ={classes.cart} aria- 
  label="Cart">
          <Badge badgeContent={100} color="primary">
            <ShoppingCartIcon/>
          </Badge>
        </IconButton>
    </AppBar>
  </div>
    );
  }
}

export default Appbar;

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

相关问题 请帮忙,当我尝试在我的 React 项目中安装材料 UI 时遇到问题 - Kindly help,am having issues when I try to install material UI in my react project 任何人都可以帮助我在反应项目中使用材料图标 - anyone can help me to use material icons in react project Material ui v4:我想使用类型脚本在 class 组件内自定义一个 Material UI 按钮。 请让我知道我该怎么做? - Material ui v4: I want to customize a Material UI button inside a class component using types script. Kindly let me know how can I do it? Material UI 自动完成下拉定位 - Material UI Autocomplete dropdown positioning 材质ui网格定位混乱 - material ui grid positioning confusion 样式材质 UI 文本字段 - Style Material UI textfield using.filter() 的问题,与 AND/OR (&amp;&amp;/||) 混淆,有人可以帮我吗? - Issue with using .filter(), confused with the AND/OR (&&/||), could someone help me out? 扩展样式材质 UI - extend style material UI 谁能帮我解释一下为什么我们需要在展示组件上使用 componentDidMount ? - Could anyone help me explain why we need componentDidMount on presentational component? 我通过 npm start 启动了我的开发服务器,然后遇到了这个错误任何人都可以帮我解决这个问题 - I started my development server by npm start and then encountered this error anyone could help me fix this Please
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM