原代码和样式:
<view style="height: 5%; margin-bottom: 10rpx; border-bottom: #4CAF50 3px solid; background-color: #4CAF50; display: flex;justify-content: center; align-items: center; flex-direction: row;">
<view style="color: white; font-size: 40rpx;">返回财务中心首页</view>
<view style="color: white; font-size: 40rpx;">周转券</view>
</view>
修改之后的样式和代码:
<view style="height: 5%; margin-bottom: 10rpx; border-bottom: #4CAF50 3px solid; background-color: #4CAF50; display: flex; align-items: center; flex-direction: row; justify-content: space-between;">
<view style="color: white; font-size: 40rpx; flex: 0 0 auto;">返回财务中心首页</view>
<view style="color: white; font-size: 40rpx; flex: 1 1 auto; text-align: center;">周转券</view>
<view style="flex: 0 0 auto;"></view> <!-- 用于保持平衡的空白视图 -->
</view>
讲解:
在这个修改中,我使用了flexbox
布局。主要的变化是:
-
在外层的
view
中,我将justify-content
属性改为space-between
。这会使得子元素分布在整个容器的两端和中间。 -
在“返回财务中心首页”的
view
中,我添加了flex: 0 0 auto;
。这样它不会伸缩并保持其自然大小,靠近容器的左边。 -
在“周转券”的
view
中,我添加了flex: 1 1 auto;
并设置了text-align: center;
。这样它会填充剩余空间,并保持文本居中。 -
我添加了一个空的
view
元素作为第三个子元素,它有flex: 0 0 auto;
属性。这是为了在右边创建一个空间,保持平衡。
这样修改后,"返回财务中心首页"将位于盒子的最左边,而"周转券"将保持在盒子的中间。
可是我想让周转券再靠左一些:
效果代码:
<view style="height: 5%; margin-bottom: 10rpx; border-bottom: #4CAF50 3px solid; background-color: #4CAF50; display: flex; align-items: center; flex-direction: row; justify-content: space-between;">
<view style="color: white; font-size: 30rpx; flex: 0 0 auto; margin-left: 20rpx;">返回财务中心首页</view>
<view style="color: white; font-size: 40rpx; flex: 1 1 auto; text-align: center; margin-right: 270rpx;">周转券</view>
<view style="flex: 0 0 auto;"></view> <!-- 用于保持平衡的空白视图 -->
</view>