<Text ohos:id="$+id:text_view" ohos:text="这是一段利用特定字体的文本" ohos:text_font="HwChinese-medium" ... /> |
Text textComponent = (Text) findComponentById(ResourceTable.Id_text_view); Typeface customFont = Typeface.createFromAsset(getContext().getAssets(), "rawfile/your_font.ttf"); textComponent.setTypeface(customFont); |
<Text ohos:id="$+id:text_view" ohos:text="字体巨细为16px" ohos:text_size="16" ... /> |
<Text ohos:id="$+id:text_view" ohos:text="字体巨细为20vp" ohos:text_size="20vp" ... /> |
<Text ohos:id="$+id:text_view" ohos:text="字体巨细为18fp" ohos:text_size="18fp" ... /> |
<Text ohos:id="$+id:text_view" ohos:text="这是斜体风格的文本" ohos:font_style="italic" ... /> |
<Text ohos:id="$+id:text_view" ohos:text="这是粗体文本" ohos:font_weight="bold" ... /> |
<Text ohos:id="$+id:text_view" ohos:text="这是更细的文本" ohos:font_weight="lighter" ... /> |
<Text ohos:id="$+id:text_view" ohos:text="赤色文本" ohos:text_color="#FF0000" ... /> |
{ "color": [ { "name": "my_red", "value": "#FF0000" } ] } |
<Text ohos:id="$+id:text_view" ohos:text="从资源文件引用的赤色文本" ohos:text_color="$color:my_red" ... /> |
<Text ohos:id="$+id:text_view" ohos:text="绿色文本" ohos:text_color="rgb(0, 255, 0)" ... /> |
<shape xmlns![]() ohos:shape="rectangle"> <solid ohos:color="#EFEFEF" /> </shape> |
<Text ohos:id="$+id:text_view" ohos:text="带有灰色背景的文本" ohos:background_element="$graphic:bg_rectangle" ... /> |
<Text ohos:id="$+id:text_view" ohos:text="右对齐文本" ohos:text_alignment="right" ... /> |
<Text ohos:id="$+id:text_view" ohos:text="垂直居中对齐文本" ohos:text_alignment="vertical_center" ... /> |
<Text ohos:id="$+id:text_view" ohos:text="居中对齐文本" ohos:text_alignment="center" ... /> |
<Text ohos:id="$+id:text_view" ohos:text="顶部左对齐文本" ohos:text_alignment="top left" ... /> |
<Text ohos:id="$+id:text_view" ohos:text="这是一个非常非常非常非常长的新闻标题,用于展示文本截断效果" ohos:truncation_mode="ellipsis_at_end" ... /> |
<Text ohos:id="$+id:text_view" ohos:text="这是一段小说内容,可能会很长,必要自动换行并且限定行数。这是一段小说内容,可能会很长,必要自动换行并且限定行数。这是一段小说内容,可能会很长,必要自动换行并且限定行数。" ohos:multiple_lines="true" ohos:max_text_lines="3" ... /> |
<Text ohos:id="$+id:text_view" ohos:text="原价100元" ohos:decoration="{type: TextDecorationType.LineThrough, color: Color.Red}" ... /> |
<Text ohos:id="$+id:text_view" ohos:text="这是带有下划线的文本" ohos:decoration="{type: TextDecorationType.Underline}" ... /> |
public class WeatherModel { private String temperature; public WeatherModel(String temperature) { this.temperature = temperature; } public String getTemperature() { return temperature; } public void setTemperature(String temperature) { this.temperature = temperature; } } |
<Text ohos:id="$+id/temperature_text" ohos:text="{{viewModel.temperature}}" ... /> |
public class MainAbilitySlice extends AbilitySlice { private WeatherModel viewModel; @Override public void onStart(Intent intent) { super.onStart(intent); // 初始化数据模型 viewModel = new WeatherModel("25℃"); // 获取结构的绑定对象 DataAbilityHelper helper = DataAbilityHelper.creator(this); AbilitySliceDataBinding binding = DataBindingUtil.inflate(helper, ResourceTable.Layout_ability_main, null, false); binding.setViewModel(viewModel); setUIContent(binding.getRoot()); // 模拟数据更新 new Handler().postDelayed(() -> { viewModel.setTemperature("26℃"); }, 3000); } } |
<Text ohos:id="$+id/login_result_text" ohos:text="请输入用户名和暗码" ... /> |
public class LoginAbilitySlice extends AbilitySlice { @Override public void onStart(Intent intent) { super.onStart(intent); setUIContent(ResourceTable.Layout_ability_login); Button loginButton = (Button) findComponentById(ResourceTable.Id_login_button); Text resultText = (Text) findComponentById(ResourceTable.Id_login_result_text); loginButton.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { // 模拟登录验证逻辑 boolean isLoginSuccess = performLogin(); if (isLoginSuccess) { resultText.setText("登录乐成"); } else { resultText.setText("用户名或暗码错误,请重试"); } } }); } private boolean performLogin() { // 现实的登录验证逻辑,这里简单返回一个固定值 return true; } } |
<Text ohos:id="$+id/news_source_text" ohos:text="来源:[详细新闻网站名称]" ... /> |
public class NewsDetailAbilitySlice extends AbilitySlice { @Override public void onStart(Intent intent) { super.onStart(intent); setUIContent(ResourceTable.Layout_ability_news_detail); Text sourceText = (Text) findComponentById(ResourceTable.Id_news_source_text); sourceText.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { // 处置惩罚点击事件,跳转到新闻来源网站 Intent intent = new Intent(); Operation operation = new Intent.OperationBuilder() .withUri("https://[详细新闻网站地址]") .build(); intent.setOperation(operation); startAbility(intent); } }); } } |
<Text ohos:id="$+id/note_text" ohos:text="这是一条便签内容" ... /> |
public class NoteAbilitySlice extends AbilitySlice { @Override public void onStart(Intent intent) { super.onStart(intent); setUIContent(ResourceTable.Layout_ability_note); Text noteText = (Text) findComponentById(ResourceTable.Id_note_text); noteText.setLongClickedListener(new Component.LongClickedListener() { @Override public boolean onLongClicked(Component component) { // 处置惩罚长按事件,弹出上下文菜单 showContextMenu(); return true; } }); } private void showContextMenu() { // 现实的上下文菜单显示逻辑,这里可以利用PopupMenu等组件实现 ToastDialog toast = new ToastDialog(getContext()); toast.setText("上下文菜单"); toast.show(); } } |
<shape xmlns![]() ohos:shape="rectangle"> <solid ohos:color="#FFFFFF" /> <corners ohos:radius="4vp" /> <states> <state ohos:state_pressed="true"> <solid ohos:color="#E0E0E0" /> </state> </states> </shape> |
<Text ohos:id="$+id/button_text" ohos:text="点击我" ohos:background_element="$graphic:button_bg" ... /> |
欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) | Powered by Discuz! X3.4 |