同事做鸿蒙的适配,横向两个控件总是有问题,不能耽误她愉快的午饭,必须出手
前言
同事做鸿蒙的适配,横向两个控件总是有问题,不能耽误她愉快的午饭,必须出手
一、可以使用DirectionalLayout
这个类似一个线性布局,有权重属性
二、使用步骤
1.嵌套
代码如下(示例):
<DirectionalLayout
ohos:height="match_parent"
ohos:width="match_parent"
ohos:orientation="horizontal"
ohos:top_margin="30vp">
<TextField
ohos:id="$+id:text_field"
ohos:height="40vp"
ohos:width="280vp"
ohos:background_element="$graphic:background_text_field"
ohos:hint="请输入您要调研的技术或问题"
ohos:hint_color="#FFFFFF"
ohos:text_alignment="center"
ohos:text_size="15vp"
/>
<Button
ohos:id="$+id:search_button"
ohos:height="40vp"
ohos:width="match_content"
ohos:background_element="$graphic:background_button"
ohos:layout_alignment="horizontal_center"
ohos:text="开始调研"
ohos:text_color="#FFFFFF"
ohos:text_size="15fp"
/>
</DirectionalLayout>
2.添加权重
代码如下(示例):
<DirectionalLayout
ohos:height="match_parent"
ohos:width="match_parent"
ohos:orientation="horizontal"
ohos:top_margin="30vp"
ohos:total_weight="1">
<TextField
ohos:id="$+id:text_field"
ohos:height="40vp"
ohos:width="280vp"
ohos:background_element="$graphic:background_text_field"
ohos:hint="请输入您要调研的技术或问题"
ohos:hint_color="#FFFFFF"
ohos:text_alignment="center"
ohos:text_size="15vp"
ohos:weight="0.7"
/>
<Button
ohos:id="$+id:search_button"
ohos:height="40vp"
ohos:width="match_content"
ohos:background_element="$graphic:background_button"
ohos:layout_alignment="horizontal_center"
ohos:text="开始调研"
ohos:text_color="#FFFFFF"
ohos:text_size="15fp"
ohos:weight="0.3"
/>
</DirectionalLayout>
该处使用 ohos:total_weight="1"表示权重的总数
每个子控件上使用ohos:weight="0.3"来分配
表示占该方向上的30%
总结
以上就是今天要讲的内容,同事高兴的去吃饭了。