HarmonyOSAPP組件分享(二)
作者:鴻蒙時代
文章由鴻蒙社區(qū)產出,想要了解更多內容請前往:51CTO和華為官方戰(zhàn)略合作共建的鴻蒙技術社區(qū)https://harmonyos.51cto.com
Button組件介紹和應用體驗分享
Button組件是常用的交互類組件之一,本節(jié)將來聊聊Button組件的使用以及按照按鈕的形狀,按鈕可以分為:普通按鈕,橢圓按鈕,膠囊按鈕,圓形按鈕等的各種的樣式圖
顯示效果:
代碼如下:
布局中的代碼:
- <?xml version="1.0" encoding="utf-8"?>
- <DirectionalLayout
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:height="match_parent"
- ohos:width="match_parent"
- ohos:orientation="vertical">
- <Button
- ohos:id="$+id:jltfbtn"
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:background_element="#FF17D3EB"
- ohos:layout_alignment="horizontal_center"
- ohos:text="我是一個按鈕button"
- ohos:padding="20vp"
- ohos:top_margin="10px"
- ohos:text_color="#FFFFFF"
- ohos:text_size="50"
- />
- <Text
- ohos:height="100px"
- ohos:width="300px"
- ohos:text_size="20fp"
- ohos:top_margin="40px"
- ohos:left_margin="400px"
- ohos:text="普通按鈕"/>
- <Button
- ohos:width="200vp"
- ohos:height="70vp"
- ohos:text_size="27fp"
- ohos:text="button"
- ohos:background_element="$graphic:jltfcolor_blue_element"
- ohos:top_margin="15px"
- ohos:left_margin="90vp"
- ohos:bottom_margin="15vp"
- ohos:right_padding="8vp"
- ohos:left_padding="8vp"
- />
- <Text
- ohos:height="100px"
- ohos:width="300px"
- ohos:text_size="20fp"
- ohos:left_margin="400px"
- ohos:text="橢圓按鈕"/>
- <Button
- ohos:width="200vp"
- ohos:height="70vp"
- ohos:text_size="27fp"
- ohos:text="button"
- ohos:background_element="$graphic:jltfoval_button_element"
- ohos:bottom_margin="15vp"
- ohos:top_margin="15px"
- ohos:left_margin="90vp"
- ohos:right_padding="8vp"
- ohos:left_padding="8vp"
- />
- <Text
- ohos:height="100px"
- ohos:width="300px"
- ohos:text_size="20fp"
- ohos:left_margin="400px"
- ohos:text="膠囊按鈕"/>
- <Button
- ohos:id="$+id:button"
- ohos:width="200vp"
- ohos:height="70vp"
- ohos:text_size="27fp"
- ohos:text="button"
- ohos:background_element="$graphic:jltfcapsule_button_element"
- ohos:left_margin="90vp"
- ohos:top_margin="15px"
- ohos:bottom_margin="15vp"
- ohos:right_padding="15vp"
- ohos:left_padding="15vp"
- />
- <Text
- ohos:height="100px"
- ohos:width="300px"
- ohos:text_size="20fp"
- ohos:left_margin="400px"
- ohos:text="圓形按鈕"/>
- <Button
- ohos:id="$+id:button2"
- ohos:width="140vp"
- ohos:height="140vp"
- ohos:text_size="27fp"
- ohos:background_element="$graphic:jltfcircle_button_element"
- ohos:text="+"
- ohos:left_margin="110vp"
- ohos:bottom_margin="15vp"
- ohos:right_padding="15vp"
- ohos:left_padding="15vp"
- />
- </DirectionalLayout>
Slice中的代碼:
- package com.example.jltftiyan.slice;
- import com.example.jltftiyan.ResourceTable;
- import ohos.aafwk.ability.AbilitySlice;
- import ohos.aafwk.content.Intent;
- import ohos.agp.components.Button;
- public class MainAbilitySlice extends AbilitySlice {
- @Override
- public void onStart(Intent intent) {
- super.onStart(intent);
- super.setUIContent(ResourceTable.Layout_ability_main);
- Button button = (Button) findComponentById(ResourceTable.Id_jltfbtn);
- button.setClickedListener(l -> {
- //更改Button組件的內容
- button.setText("我被點擊了~");
- });
- }
- @Override
- public void onActive() {
- super.onActive();
- }
- @Override
- public void onForeground(Intent intent) {
- super.onForeground(intent);
- }
- }
graphic目錄下xml文件的代碼示例如下:
- jltfcolor_blue_element.xml
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:shape="rectangle">
- <solid
- ohos:color="#007CFD"/>
- </shape>
- jltfoval_button_element.xml
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:shape="oval">
- <solid
- ohos:color="#007CFD"/>
- </shape>
- jltfcapsule_button_element.xml
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:shape="rectangle">
- <corners
- ohos:radius="100"/>
- <solid
- ohos:color="#007CFD"/>
- </shape>
- jltfcircle_button_element.xml
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:shape="oval">
- <solid
- ohos:color="#007CFD"/>
- </shape>
StackLayout布局練習與分享
StackLayout直接在屏幕上開辟出一塊空白的區(qū)域,添加到這個布局中的視圖都是以層疊的方式顯示,而它會把這些視圖默認放到這塊區(qū)域的左上角,第一個添加到布局中視圖顯示在最底層,最后一個被放在最頂層。上一層的視圖會覆蓋下一層的視圖。
自己可以通過修改寬高設置大小來控制位置的變換,后面會有更多新的內容呈現(xiàn)出來,這里就簡單的敲了個案例。
代碼如下:
- <?xml version="1.0" encoding="utf-8"?>
- <StackLayout
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:id="$+id:stack_layout"
- ohos:height="match_parent"
- ohos:width="match_parent">
- <Text
- ohos:id="$+id:text_blue"
- ohos:text_alignment="bottom|right"
- ohos:text_size="24fp"
- ohos:text="第四層"
- ohos:height="match_parent"
- ohos:width="match_parent"
- ohos:background_element="#3F56EA" />
- <Text
- ohos:id="$+id:text_light_purple"
- ohos:text_alignment="bottom|right"
- ohos:text_size="24fp"
- ohos:text="第三層"
- ohos:height="300vp"
- ohos:width="match_parent"
- ohos:background_element="#00AAEE" />
- <Text
- ohos:id="$+id:text_light_lianxi"
- ohos:text_alignment="bottom|horizontal_center"
- ohos:text_size="24fp"
- ohos:text="第二層"
- ohos:height="match_parent"
- ohos:width="150vp"
- ohos:background_element="#FF74FF8B" />
- <Text
- ohos:id="$+id:text_orange"
- ohos:text_alignment="center"
- ohos:text_size="24fp"
- ohos:text="第一層"
- ohos:height="80vp"
- ohos:width="match_parent"
- ohos:background_element="#00BFC9" />
- </StackLayout>
責任編輯:jianghua
來源:
鴻蒙社區(qū)