如何在屏幕上按比例显示四幅图像。

huangapple go评论82阅读模式
英文:

How to display 4 images positioned proportionally on screen

问题

我想要在屏幕上按比例显示4张图片,但我对CSS不太擅长,所以无法弄清楚如何做。我尝试使用flex,将其设置为1,然后在子视图中将其设置为0.25,但不起作用,甚至尝试了flexWrap,仍然没有效果,它始终在一行中,因为我将flexDirection设置为row

<View style={getStyle(styles, ["container"], this.props.theme)}>
    <View style={styles.itemContainer}>
        <TouchableHighlight
            onPress={() => this.onHandlePress("DevicesNavigator")}
        >
            <Icon name="pencil" color="yellow" size={100} />
        </TouchableHighlight>
        <Text style={styles.imageText}>
            {strings("initialTab.devicesTab")}
        </Text>
    </View>
    <View style={styles.itemContainer}>
        <TouchableHighlight
            onPress={() => this.onHandlePress("ReportsNavigatior")}
        >
            <Icon name="tablet" color="yellow" size={100} />
        </TouchableHighlight>
        <Text style={styles.imageText}>{strings("initialTab.reports")}</Text>
    </View>
    <View style={styles.itemContainer}>
        <TouchableHighlight
            onPress={() => this.onHandlePress("SyncNavigatior")}
        >
            <Icon name="refresh" color="yellow" size={100} />
        </TouchableHighlight>
        <Text style={styles.imageText}>{strings("initialTab.sync")}</Text>
    </View>
    <View style={styles.itemContainer}>
        <TouchableHighlight
            onPress={() => this.onHandlePress("SettingsNavigatior")}
        >
            <Icon name="gear" color="yellow" size={100} />
        </TouchableHighlight>
        <Text style={styles.imageText}>{strings("initialTab.settings")}</Text>
    </View>
</View>

样式

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: "center",
        alignContent: "center",
        flexDirection: "row"
    },
    containerLIGHT_THEME: {
        backgroundColor: colors.initialFourScreenBackgroundColorLight
    },
    containerDARK_THEME: {
        backgroundColor: colors.initialFourScreenBackgroundColorDark
    },
    itemContainer: {
        flex: 0.25
    },
    imageText: {
        color: "green"
    }
});

显示

如何在屏幕上按比例显示四幅图像。

英文:

I want 4 images to be proportionally displayed on screen and I am really bad with CSS generally, so I can't figure out here how to do it. I was trying with flex, to set it on 1 and then on sub-views to set it on 0.25, but it doesn't work, even tried flexWrap, still nothing, it's always in one row, as I set flexDirection to be row

&lt;View style={getStyle(styles, [&quot;container&quot;], this.props.theme)}&gt;
    &lt;View style={styles.itemContainer}&gt;
      &lt;TouchableHighlight
        onPress={() =&gt; this.onHandlePress(&quot;DevicesNavigator&quot;)}
      &gt;
        &lt;Icon name=&quot;pencil&quot; color=&quot;yellow&quot; size={100} /&gt;
      &lt;/TouchableHighlight&gt;
      &lt;Text style={styles.imageText}&gt;
        {strings(&quot;initialTab.devicesTab&quot;)}
      &lt;/Text&gt;
    &lt;/View&gt;
    &lt;View style={styles.itemContainer}&gt;
      &lt;TouchableHighlight
        onPress={() =&gt; this.onHandlePress(&quot;ReportsNavigatior&quot;)}
      &gt;
        &lt;Icon name=&quot;tablet&quot; color=&quot;yellow&quot; size={100} /&gt;
      &lt;/TouchableHighlight&gt;
      &lt;Text style={styles.imageText}&gt;{strings(&quot;initialTab.reports&quot;)}&lt;/Text&gt;
    &lt;/View&gt;
    &lt;View style={styles.itemContainer}&gt;
      &lt;TouchableHighlight
        onPress={() =&gt; this.onHandlePress(&quot;SyncNavigatior&quot;)}
      &gt;
        &lt;Icon name=&quot;refresh&quot; color=&quot;yellow&quot; size={100} /&gt;
      &lt;/TouchableHighlight&gt;
      &lt;Text style={styles.imageText}&gt;{strings(&quot;initialTab.sync&quot;)}&lt;/Text&gt;
    &lt;/View&gt;
    &lt;View style={styles.itemContainer}&gt;
      &lt;TouchableHighlight
        onPress={() =&gt; this.onHandlePress(&quot;SettingsNavigatior&quot;)}
      &gt;
        &lt;Icon name=&quot;gear&quot; color=&quot;yellow&quot; size={100} /&gt;
      &lt;/TouchableHighlight&gt;
      &lt;Text style={styles.imageText}&gt;{strings(&quot;initialTab.settings&quot;)}&lt;/Text&gt;
    &lt;/View&gt;
  &lt;/View&gt;

And style:

  container: {
    flex: 1,
    justifyContent: &quot;center&quot;,
    alignContent: &quot;center&quot;,
    flexDirection: &quot;row&quot;
  },
  containerLIGHT_THEME: {
    backgroundColor: colors.initialFourScreenBackgroundColorLight
  },
  containerDARK_THEME: {
    backgroundColor: colors.initialFourScreenBackgroundColorDark
  },
  itemContainer: {
    flex: 0.25
  },
  imageText: {
    color: &quot;green&quot;
  }
});

Display

如何在屏幕上按比例显示四幅图像。

答案1

得分: 0

Sure, here's the translated code snippet:

.container {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  width: 1000px;
}

.item {
  width: 50%;
}

.item img {
  width: 100%;
}
<div class="container">
  <div class="item">item 1</div>
  <div class="item">item 2</div>
  <div class="item">item 3</div>
  <div class="item">item 4</div>
</div>

Please note that the URLs and code comments remain in their original language.

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.container {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  width: 1000px;
}

.item {
  width: 50%;
}

.item img {
  width: 100%;
}

<!-- language: lang-html -->

&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;item&quot;&gt;item 1&lt;/div&gt;
  &lt;div class=&quot;item&quot;&gt;item 2&lt;/div&gt;
  &lt;div class=&quot;item&quot;&gt;item 3&lt;/div&gt;
  &lt;div class=&quot;item&quot;&gt;item 4&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

You can set the width of the container & item, thereby creating a break point and wrap the items, here is a JSFiddle with dummy images
https://jsfiddle.net/ah1ws6oj/

答案2

得分: 0

以下是您要翻译的内容:

这很简单,我使用了简单的 flex 布局,并使用一些移动端的 媒体查询

解决方案如下:

.parent {
  display: flex;
  flex-wrap: wrap;
}

.child {
  flex: 1 0 21%;
  margin: 5px;
  height: 100px;
  background-color: blue;
}

@media only screen and (max-width: 600px) {
 .child {
  flex: 1 0 35%;
 }
}
<div class="parent">
  <div class="child"></div>
  <div class="child"></div>
  <div class="child"></div>
  <div class="child"></div>
  <div class="child"></div>
  <div class="child"></div>
  <div class="child"></div>
  <div class="child"></div>
</div>

使用 flex-grow: 1 在 flex 简写中定义 参考链接

在您的情况中,.child 等于 .itemContainer.parent 等于 .container

我更新了这个示例 链接

英文:

Its simple i use Simple flex with some media queries for mobile

Solution Below

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.parent {
  display: flex;
  flex-wrap: wrap;
}

.child {
  flex: 1 0 21%; 
  margin: 5px;
  height: 100px;
  background-color: blue;
}

@media only screen and (max-width: 600px) {
 .child {
  flex: 1 0 35%;
}
}

<!-- language: lang-html -->

&lt;div class=&quot;parent&quot;&gt;
  &lt;div class=&quot;child&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

> With flex-grow: 1 defined in the flex shorthand [Ref]

(https://www.w3schools.com/css/css3_flexbox.asp)

In your case .child = .itemContainer and .parent = .container

I updated this example https://stackoverflow.com/questions/29546550/flexbox-4-items-per-row/45384426

答案3

得分: 0

我成功让它工作。

样式

const styles = StyleSheet.create({
  container: {
    flex: 1,
    flexWrap: "wrap",
    flexDirection: "row"
  },
  containerLIGHT_THEME: {
    backgroundColor: colors.initialFourScreenBackgroundColorLight
  },
  containerDARK_THEME: {
    backgroundColor: colors.initialFourScreenBackgroundColorDark
  },
  itemContainer: {
    marginTop: "15%",
    marginLeft: "20%"
  },
  imageText: {
    color: "green"
  }
});

使其具有flex-wrap,如其他人建议的,使其在row中,然后只设置边距。

英文:

I managed to make it work.

STYLE

const styles = StyleSheet.create({
  container: {
    flex: 1,
    flexWrap: &quot;wrap&quot;,
    flexDirection: &quot;row&quot;
  },
  containerLIGHT_THEME: {
    backgroundColor: colors.initialFourScreenBackgroundColorLight
  },
  containerDARK_THEME: {
    backgroundColor: colors.initialFourScreenBackgroundColorDark
  },
  itemContainer: {
    marginTop: &quot;15%&quot;,
    marginLeft: &quot;20%&quot;
  },
  imageText: {
    color: &quot;green&quot;
  }
});

Make it flex-wrap, as others suggested, make it go in row, and just set margins.

huangapple
  • 本文由 发表于 2020年1月6日 17:28:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609579.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定