日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

android 一个字符串分两行显示_【Android】DataBindinglt;中gt;

發(fā)布時間:2025/4/5 Android 57 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 一个字符串分两行显示_【Android】DataBindinglt;中gt; 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

DataBindingUtil類

DataBinding不僅可以綁定Activity還可以綁定視圖內(nèi)容(View)

// 視圖
static?extends?ViewDataBinding> T bind(View root)static?extends?ViewDataBinding> T bind(View root,
?????????????????????????????????????????????DataBindingComponent bindingComponent)// 布局static?extends?ViewDataBinding> T inflate(LayoutInflater inflater,
????????????????????????????????????????????????int layoutId,
????????????????????????????????????????????????ViewGroup parent,boolean?attachToParent, DataBindingComponent bindingComponent) // 組件static?extends?ViewDataBinding> T inflate(LayoutInflater inflater,
????????????????????????????????????????????????int layoutId,
????????????????????????????????????????????????ViewGroup parent,boolean?attachToParent)// activitystatic?extends?ViewDataBinding> T setContentView(Activity activity,
???????????????????????????????????????????????????????int layoutId)static?extends?ViewDataBinding> T setContentView(Activity activity,
???????????????????????????????????????????????????????int layoutId, DataBindingComponent bindingComponent)

檢索視圖是否被綁定, 如果沒有綁定返回null

static?extends?ViewDataBinding> T getBinding(View view)// 和getBinding不同的是如果視圖沒有綁定會去檢查父容器是否被綁定static?extends?ViewDataBinding> T findBinding(View view)

其他方法:

// 根據(jù)傳的BR的id來返回字符串類型. 可能用于日志輸出
static?String convertBrIdToString(int?id)

DataBindingComponent

每個DataBinding都可以擁有一個組件或者說設(shè)置一個默認的全局組件?

創(chuàng)建一個Component的步驟:?

1、創(chuàng)建一個MyDataBindingComponent實現(xiàn)接口DataBindingComponent?

2、創(chuàng)建MyBindingAdapter類, 用@BindingAdapter修飾其成員方法(不需要靜態(tài))?

3、在MyDataBindingComponent寫入一個get**方法()來返回該MyBindingAdapter

import?androidx.databinding.DataBindingComponent;

public?class?MyDataBindingComponent?implements?DataBindingComponent?{

????private?MyBindingAdapter mAdapter = new?TestBindingAdapter();

????@Override
????public?MyBindingAdapter getMyBindingAdapter()?{
????????return?mAdapter;
????}
}public?class?TestBindingAdapter?extends?MyBindingAdapter?{

????@Overridepublic?void?setText(TextView view, String value) {
????????view.setText(value);
????}

????@Overridepublic?void?setTextColor(TextView view, int?value) {
????????view.setTextColor(value);
????}
}import?android.widget.TextView;

import?androidx.databinding.BindingAdapter;

public?abstract?class?MyBindingAdapter?{

????@BindingAdapter("android:text")
????public?abstract?void?setText(TextView view, String value);

????@BindingAdapter("android:textColor")
????public?abstract?void?setTextColor(TextView view, int?value);
}

設(shè)置默認組件都是由DataBindingUtil設(shè)置, 但是方法也有所不同

static?DataBindingComponent getDefaultComponent()static?void?setDefaultComponent(DataBindingComponent bindingComponent)

這種設(shè)置必須在綁定視圖之前設(shè)置, 并且是默認全局的, 只需要設(shè)置一次.

使用:

MyDataBindingComponent bindingComponent = new?MyDataBindingComponent();
DataBindingUtil.setDefaultComponent(bindingComponent);
viewDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_data_binding_test);

或者這樣寫:

MyDataBindingComponent bindingComponent = new?MyDataBindingComponent();
viewDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_data_binding_test, bindingComponent);

布局中可以這樣使用:

<TextViewandroid:layout_width="match_parent"android:layout_height="50dp"android:text="@{@string/app_name}"android:textColor="@{@color/colorPrimary}"/>

代碼可以這樣設(shè)置:

bindingComponent.getMyBindingAdapter().setText(viewDataBinding.iv1,"替換值");
bindingComponent.getMyBindingAdapter().setTextColor(viewDataBinding.iv1, Color.RED);

使用場景有:

  • 換膚

  • 打點

  • 替換原生屬性

  • 等等

字符串格式化顯示

@{}中使用字符串, 可以使用三種方式

android:text='@{"loaderman"}'
android:text="@{`loaderman`}"
android:text="@{@string/name}"

同樣支持@color或@drawable

格式化顯示:

<string?name="string_format">名字: %s 性別: %sstring>

布局中:

android:text="@{@string/string_format(`loaderman`, `男`)}"

默認值

如果Variable還沒有復(fù)制就會使用默認值顯示:

android:text="@{user.age, default=`30`}"

上下文

DataBinding本身提供了一個名為context的Variable. 可以直接使用. 等同于View的getContext().

android:text="@{context.getApplicationInfo().toString()}"

引用其他控件

<TextViewandroid:id="@+id/tv_name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_marginLeft="8dp"android:text="我是名字叫l(wèi)oaderman"
/>
?<TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_marginLeft="8dp"android:text="@{tvName.text}"
/>

注:引用包含_的控件id是可以直接忽略該符號. 例如tv_name直接寫tvName.

表達式和轉(zhuǎn)義字符

@{}里面除了可以執(zhí)行方法以外還可以寫表達式, 并且支持一些特有表達式

  • 算術(shù) + - / * %

  • 字符串合并 +

  • 邏輯 && ||

  • 二元 & | ^

  • 一元 + - ! ~

  • 移位 >> >>> <<

  • 比較 == > < >= <=

  • Instanceof

  • Grouping ()

  • 文字 - character, String, numeric, null

  • Cast

  • 方法調(diào)用

  • Field 訪問

  • Array 訪問 []

  • 三元 ?:

避免空指針

variable的值即使設(shè)置null或者沒有設(shè)置也不會出現(xiàn)空指針異常. 這是因為官方已經(jīng)用DataBinding的@BindingAdapter注解重寫了很多屬性. 并且里面進行了判空處理.

//下面不會出現(xiàn)空指針異常.
dataBinding.setUserName(null);

//支持特有的非空多元表達式
android:text="@{user.displayName ?? user.lastName}"
//上面也等價于
android:text="@{user.displayName != null ? user.displayName : user.lastName}"

集合和轉(zhuǎn)義字符

集合不屬于java.lang*下, 需要導(dǎo)入全路徑.

??????????name="list"
??????????type="java.util.List>String/>??????????name="map"type="java.util.Map"/>

上面的寫法會下面報錯:

Error:與元素類型 "variable"?相關(guān)聯(lián)的 "type"?屬性值不能包含 '?字符。

因為

常用的轉(zhuǎn)移字符有:

空格 &nbsp;160;
< 小于號 &lt;60;
> 大于號 &gt;62;
& 與號 &amp;38;
” 引號 &quot;34;
‘ 撇號 &apos;39;
× 乘號 &times;215;
÷ 除號 &divide;247;

正確寫法:

??????????name="list"
??????????type="java.util.List>String/>??????????name="map"type="java.util.Map>String, String/>

集合和數(shù)組都可以用[]來得到元素

android:text="@{map["firstName"]}"

@InverseMethod--轉(zhuǎn)換器

在android studio3.0提供inverse系列的新注解, 全部都是針對數(shù)據(jù)雙向綁定. 在數(shù)據(jù)和視圖的數(shù)據(jù)不統(tǒng)一時可以使用該注解@InverseMethod解決數(shù)據(jù)轉(zhuǎn)換的問題?

例如數(shù)據(jù)模型存儲用戶的id但是視圖不顯示id而是顯示用戶名(數(shù)據(jù)和視圖的類型不一致), 我們就需要在兩者之間轉(zhuǎn)換.?

需要創(chuàng)建public static兩個方法, 我們簡稱為”轉(zhuǎn)換方法(convertion method)”和”反轉(zhuǎn)方法(inverse method)”?

  • 轉(zhuǎn)換方法與反轉(zhuǎn)方法的參數(shù)數(shù)量必須相同?

  • 轉(zhuǎn)換方法的最終參數(shù)的類型與反轉(zhuǎn)方法的返回值必須相同?

轉(zhuǎn)換函數(shù): 是刷新視圖的時候使用 (決定視圖顯示數(shù)據(jù)) 會回調(diào)兩次, 可以理解為getter函數(shù);?

反轉(zhuǎn)函數(shù): 是刷新數(shù)據(jù)的時候使用 (決定實體存儲數(shù)據(jù)), 可以理解為setter函數(shù); 簡單示例: 在用戶id和用戶名之間轉(zhuǎn)換. 存儲id但是顯示的時候顯示用戶名

寫一個自定義轉(zhuǎn)換器類:

import?android.content.res.ColorStateList;
import?android.graphics.drawable.ColorDrawable;
import?android.widget.TextView;

import?androidx.databinding.BindingConversion;
import?androidx.databinding.InverseMethod;

public?class?Converters?{
??

????@InverseMethod("toName")
????public?static?String toName(TextView view, int??id)?{
????????if?(id == 888) {
????????????return?"loaderman";
????????}
????????return?"NaN";
????}

????@InverseMethod("toID")
????public?static?String toID(TextView view, String name)?{
????????if?(name.equals("loaderman")) {
????????????return?888+"";
????????}
????????return?"NaN";
????}
}

布局中:

<data>
????????<import?type="com.loaderman.frameappdemo.databinding.Converters"?/>
data>????????????android:id="@+id/iv1"
????????????android:layout_width="200dp"
????????????android:layout_height="wrap_content"
????????????android:text="@{Converters.toName(iv1,id)}"?/>

??????????????android:id="@+id/iv2"
????????????android:layout_width="200dp"
????????????android:layout_height="wrap_content"
????????????android:text="@{Converters.toID(iv2,name)}"?/>

@BindingConversion屬性值自動進行類型轉(zhuǎn)換

  • 只能修飾public static方法.?

  • 任意位置任意方法名都不限制?

  • DataBinding自動匹配被該注解修飾的方法和匹配參數(shù)類型?

  • 返回值類型必須和屬性setter方法匹配, 且參數(shù)只能有一個?

  • 要求屬性值必須是@{}DataBinding表達式

public?class?Converters?{
????@BindingConversion
????public?static?ColorDrawable convertColorToDrawable(int?color)?{
????????return?new?ColorDrawable(color);
????}

????@BindingConversion
????public?static?ColorStateList convertColorToColorStateList(int?color)?{
????????return?ColorStateList.valueOf(color);
????}

}

設(shè)置布局中TextView的背景:

android:background="@{`loaderman`}"

可以看到我給背景隨意設(shè)置一個字符串. 這樣就不會匹配Background的int參數(shù)類型. 然后DataBinding就會檢索匹配該類型的@BindingConversion方法. 然后轉(zhuǎn)換.?

注意:android:text如果想用int自動轉(zhuǎn)String是不可以的, 因為int值會被識別為resource id. @BindingConversion無法工作.

控制組件隱藏顯示實現(xiàn)

布局data標(biāo)簽中:

<variablename="isVisible"type="Boolean"?/>
<import?type="android.view.View"?/>

綁定

<TextViewandroid:id="@+id/tv_test"android:layout_width="200dp"android:layout_height="wrap_content"android:text='隱藏顯示'android:visibility="@{isVisible? View.GONE:View.VISIBLE}"?/>

頁面中測試:

viewDataBinding.btn.setOnClickListener(new?View.OnClickListener() {
????????????@Override
????????????public?void?onClick(View v)?{
????????????????isVisible = !isVisible;
????????????????viewDataBinding.setIsVisible(isVisible);
????????????}
});

完整示例

MyBindingAdapter:

import?android.widget.TextView;

import?androidx.databinding.BindingAdapter;

public?abstract?class?MyBindingAdapter?{

????@BindingAdapter("android:text")
????public?abstract?void?setText(TextView view, String value);

????@BindingAdapter("android:textColor")
????public?abstract?void?setTextColor(TextView view, int?value);
}

TestBindingAdapter:

public?class?TestBindingAdapter?extends?MyBindingAdapter?{

????@Overridepublic?void?setText(TextView view, String value) {
????????view.setText(value);
????}

????@Overridepublic?void?setTextColor(TextView view, int?value) {
????????view.setTextColor(value);
????}
}

MyDataBindingComponent:

public?class?MyDataBindingComponent?implements?DataBindingComponent?{

????private?MyBindingAdapter mAdapter = new?TestBindingAdapter();

????@Override
????public?MyBindingAdapter getMyBindingAdapter()?{
????????return?mAdapter;
????}
}

activity的頁面布局:

xml version="1.0"?encoding="utf-8"?>
<layout?xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools">

????<data>

????????<import?type="com.loaderman.frameappdemo.databinding.Converters"?/>

????????<variablename="name"type="String"?/>

????????<variablename="id"type="int"?/>

????????<variablename="tel"type="String"?/>

????????<variablename="isVisible"type="Boolean"?/>

????????<import?type="android.view.View"?/>
????data>

????<LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".databinding.DataBindingTestActivity">


????????<TextViewandroid:id="@+id/iv1"android:layout_width="200dp"android:layout_height="wrap_content"android:text="@{Converters.toName(iv1,id)}"?/>

????????<TextViewandroid:id="@+id/iv2"android:layout_width="200dp"android:layout_height="wrap_content"android:text="@{Converters.toID(iv2,name)}"?/>

????????<TextViewandroid:layout_width="200dp"android:layout_height="wrap_content"android:text='@{"張三"}'?/>

????????<TextViewandroid:id="@+id/tv_test"android:layout_width="200dp"android:layout_height="wrap_content"android:text='隱藏顯示'android:visibility="@{isVisible? View.GONE:View.VISIBLE}"?/>

????????<TextViewandroid:layout_width="200dp"android:layout_height="wrap_content"android:text='@{tel==null?"未填寫電話":tel,default=`沒有電話,我是默認值`}'?/>
????????<TextViewandroid:layout_width="200dp"android:layout_height="wrap_content"android:text='@{tel,default="沒有電話,我是默認值"}'?/>

????????<TextViewandroid:layout_width="200dp"android:layout_height="wrap_content"android:text="@{`李四`}"?/>

????????<TextViewandroid:layout_width="200dp"android:layout_height="wrap_content"android:text="@{@string/string_format(`王五`, `男`)}"?/>
????????<Buttonandroid:id="@+id/btn"android:layout_width="match_parent"android:text="點擊控制隱藏顯示"android:layout_height="wrap_content"/>
????????<TextViewandroid:id="@+id/tv_name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_marginLeft="8dp"android:text="我是名字叫l(wèi)oaderman"
????????????/>
????????<TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_marginLeft="8dp"android:text="@{tvName.text}"
????????????/>
????????<TextViewandroid:layout_width="match_parent"android:layout_height="50dp"android:text="@{@string/app_name}"android:textColor="@{@color/colorPrimary}"/>

????????<TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_marginLeft="8dp"android:text="@{context.getApplicationInfo().toString()}"
????????????/>
????LinearLayout>
layout>public?class?MyApplication?extends?Application?{
????public?static?Context context;

????@Override
????public?void?onCreate()?{
????????super.onCreate()
????????DataBindingUtil.setDefaultComponent(new?MyDataBindingComponent());
????}

???
}

activity測試:

import?androidx.appcompat.app.AppCompatActivity;
import?androidx.databinding.DataBindingUtil;

import?android.graphics.Color;
import?android.os.Bundle;
import?android.view.View;

import?com.loaderman.frameappdemo.R;

public?class?DataBindingTestActivity?extends?AppCompatActivity?{

????private?ActivityDataBindingTestBinding viewDataBinding;
????private?boolean?isVisible = false;

????@Override
????protected?void?onCreate(Bundle savedInstanceState)?{
????????super.onCreate(savedInstanceState);

????????viewDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_data_binding_test);
????????viewDataBinding.setId(888);
????????viewDataBinding.setName("loaderman");
????????viewDataBinding.btn.setOnClickListener(new?View.OnClickListener() {
????????????@Override
????????????public?void?onClick(View v)?{
????????????????isVisible = !isVisible;
????????????????viewDataBinding.setIsVisible(isVisible);
????????????????MyDataBindingComponent bindingComponent = (MyDataBindingComponent) DataBindingUtil.getDefaultComponent();
????????????????bindingComponent.getMyBindingAdapter().setText(viewDataBinding.iv1,"替換值");
????????????????bindingComponent.getMyBindingAdapter().setTextColor(viewDataBinding.iv1, Color.RED);
????????????}
????????});

????}
}

測試效果:

? 碼上加油站

? 一起來加油

長按掃碼關(guān)注

記得點個在看哦!

總結(jié)

以上是生活随笔為你收集整理的android 一个字符串分两行显示_【Android】DataBindinglt;中gt;的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。