不常用
在PositionLayout中,子组件通过指定准确的x/y坐标值在屏幕上显示。(0, 0)为左上角;当向下或向右移动时,坐标值变大;允许组件之间互相重叠
布局方式
PositionLayout以坐标的形式控制组件的显示位置,允许组件相互重叠。
- 在layout目录下的XML文件中创建PositionLayout并添加多个组件,并通过position_x和position_y属性设置子组件的坐标
<?xml version="1.0" encoding="utf-8"?>
<PositionLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
>
<Text
ohos:id="$+id:text_1"
ohos:height="match_content"
ohos:width="match_content"
ohos:background_element="#FF485DEC"
ohos:layout_alignment="horizontal_center"
ohos:text="科技"
ohos:text_size="40vp"
/>
</PositionLayout>
Text text = (Text) findComponentById(ResourceTable.Id_text_1);
设置显示位置
text.setContentPosition(200,300);
package com.example.myapplication.slice;
import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Text;
public class MainAbilitySlice extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
Text text = (Text) findComponentById(ResourceTable.Id_text_1);
text.setContentPosition(200,300);
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}