每天進(jìn)步一點(diǎn)點(diǎn),成功在久不在速
滑動(dòng)選擇器
?
?
?//設(shè)置文本樣式
picker.setNormalTextFont(Font.DEFAULT_BOLD);
picker.setNormalTextSize(40);
picker.setNormalTextColor(new Color(Color.getIntColor("#FFA500")));
picker.setSelectedTextFont(Font.DEFAULT_BOLD);
picker.setSelectedTextSize(40);
picker.setSelectedTextColor(new Color(Color.getIntColor("#00FFFF")));
//設(shè)置邊框
ShapeElement shape = new ShapeElement();
shape.setShape(ShapeElement.RECTANGLE);
shape.setRgbColor(RgbColor.fromArgbInt(0xFF40E0D0));
// 單獨(dú)設(shè)置上邊框
// picker.setDisplayedLinesTopElement(shape);?
// 單獨(dú)設(shè)置下邊框
// picker.setDisplayedLinesBottomElement(shape);
// 同時(shí)設(shè)置上下邊框
picker.setDisplayedLinesElements(shape, shape);?
設(shè)置著色器
<Picker ohos:id="$+id:picker2" ohos:height="match_content" ohos:width="match_parent" ohos:normal_text_size="20vp" ohos:selected_text_size="26vp" ohos:top_margin="10vp" ohos:max_value="33" ohos:min_value="16" ohos:background_element="#686EA8EF" ohos:shader_color="#FF1EFF34" />
ability_main.xml文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-806926.html
<?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:alignment="center"
ohos:orientation="vertical">
<Text
ohos:id="$+id:text_helloworld3"
ohos:height="match_content"
ohos:width="match_content"
ohos:background_element="$graphic:background_ability_main"
ohos:layout_alignment="horizontal_center"
ohos:text="每天進(jìn)步一點(diǎn)點(diǎn)"
ohos:text_size="40vp"
/>
<Text
ohos:id="$+id:text_helloworld3a"
ohos:height="match_content"
ohos:width="match_content"
ohos:background_element="#FFA5F383"
ohos:layout_alignment="horizontal_center"
ohos:text="成功在久不在速"
ohos:text_size="40vp"
/>
<Text
ohos:id="$+id:text_helloworld"
ohos:height="match_content"
ohos:width="match_content"
ohos:background_element="$graphic:background_ability_main"
ohos:layout_alignment="horizontal_center"
ohos:text="$string:mainability_HelloWorld"
ohos:text_size="40vp"
/>
<Text
ohos:id="$+id:text_helloworld2"
ohos:height="match_content"
ohos:width="match_content"
ohos:background_element="#FFA5F383"
ohos:layout_alignment="horizontal_center"
ohos:text="滑動(dòng)選擇器"
ohos:text_size="40vp"
/>
<Picker
ohos:id="$+id:picker2"
ohos:height="match_content"
ohos:width="match_parent"
ohos:normal_text_size="20vp"
ohos:selected_text_size="26vp"
ohos:top_margin="10vp"
ohos:max_value="33"
ohos:min_value="16"
ohos:background_element="#686EA8EF"
ohos:shader_color="#FF1EFF34"
/>
<Picker
ohos:id="$+id:picker2a"
ohos:height="match_content"
ohos:width="match_parent"
ohos:normal_text_size="20vp"
ohos:selected_text_size="26vp"
ohos:top_margin="10vp"
ohos:background_element="#686EA8EF"
/>
</DirectionalLayout>
?MainAbilitySlice.java文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-806926.html
package com.example.myapplication.slice;
import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.Color;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Picker;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ShapeElement;
public class MainAbilitySlice extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
//對(duì)滑動(dòng)選擇器進(jìn)行配置
Picker picker = (Picker) findComponentById(ResourceTable.Id_picker2);
picker.setCompoundElementPadding(50);//設(shè)置文本左右兩側(cè)的圖形元素邊距
picker.setSelectorItemNum(3);//選項(xiàng)顯示的數(shù)量
設(shè)置文本左右兩側(cè)的圖形元素
picker.setElementFormatter(new Picker.ElementFormatter() {
@Override
public Element leftElement(int i) { //左側(cè)
ShapeElement element = new ShapeElement();
element.setRgbColor(new RgbColor(0,0,255));
return element;
}
@Override
public Element rightElement(int i) {
return null;
}
});
//將索引轉(zhuǎn)換為格式化文本
picker.setFormatter(new Picker.Formatter() {
@Override
public String format(int i) {
return "選項(xiàng):"+i;
}
});
//批量設(shè)置字符串方式顯示
Picker picker2a = (Picker) findComponentById(ResourceTable.Id_picker2a);
picker2a.setCompoundElementPadding(50);//設(shè)置文本左右兩側(cè)的圖形元素邊距
picker2a.setSelectorItemNum(3);//選項(xiàng)顯示的數(shù)量
picker2a.setDisplayedData(new String[]{"上海","北京","山東","江蘇","海南"});
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
到了這里,關(guān)于17.鴻蒙HarmonyOS App(JAVA)滑動(dòng)選擇器的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!