하드 코딩 된 문자열이있는 TextView가 있고이 문자열 끝에 넣을 동적 변수가 있습니다. 이것은 내 코드입니다.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<TextView
android:id="@+id/PeopleName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/Generic_Text"+"@{ Profile.name }" />
</LinearLayout>
에 문제가 android:text="@string/Generic_Text"+"@{ Profile.name }"
있습니다. Generic_Text
상태는 다음이 “내 이름은” Profile.name
동적이며 분명히 프로필 프로필에서 변경합니다. 전체 TextView 출력이 My Name is {Profile.name}이 되도록하고 싶습니다 . 어떤 도움이라도 좋을 것입니다.
답변
다음과 같이 할 수 있습니다.
android:text= "@{String.format(@string/Generic_Text, Profile.name)}"
문자열에 문자열 형식을 사용하는 경우 Generic_Text
. 전의. %s
끝에
답변
이 작업을 더 간단하게 수행 할 수 있습니다.
android:text= "@{@string/generic_text(profile.name)}"
문자열은 다음과 같아야합니다.
<string name="generic_text">My Name is %s</string>
편집하다:
-
물론 필요한만큼의 변수를 사용할 수 있습니다.
android:text= "@{@string/generic_text(profile.firstName, profile.secondName)}" <string name="generic_text">My Name is %1$s %2$s</string>
-
데이터 바인딩으로 설계 되었기 때문에 작동합니다. 더 많은 문서 : https://developer.android.com/topic/libraries/data-binding/expressions#resources
답변
문자열을 연결하는 다양한 방법
1. 문자열 리소스 사용 ( 현지화로 인해 권장 됨 )
android:text= "@{@string/generic_name(user.name)}"
이렇게 문자열 리소스를 만드십시오.
<string name="generic_name">Hello %s</string>
2. 하드 코딩 된 연결
android:text="@{`Hello ` + user.name}"/>
전화 번호에 +와 같이 하드 코드 된 추가가 필요할 때 유용합니다.
3. String
의 concat 메서드 사용
android:text="@{user.firstName.concat(@string/space).concat(user.lastName)}"
다음 space
은 내부에 배치 된 html 엔티티입니다 strings.xml
. 때문에 XML
직접 HTML을 엔티티 또는 특수 문자를 허용하지 않습니다. (HTML 엔터티 연결)
<string name="space">\u0020</string>
4. 사용 String.format()
android:text= "@{String.format(@string/Hello, user.name)}"
이 유형의 레이아웃에서 String 클래스를 가져와야합니다.
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<import type="String" />
</data>
<TextView
android:text= "@{String.format(@string/Hello, user.name)}"
... >
</TextView>
</layout>
5. 문자열 리소스로 두 문자열을 연결합니다.
android:text="@{@string/generic_name(user.firstName,user.lastName)}"
이 경우 문자열 리소스를 strings.xml
<string name="generic_name">%1$s, %2$s</string>
다른 많은 방법이있을 수 있습니다. 필요한 것을 선택하십시오.
답변
바인딩 어댑터를 사용하십시오 .
이 샘플은 Kotlin으로 작성되었으며 바인딩 된 변수가 null 일 수 있음을 고려합니다.
@BindingAdapter("my_name")
fun TextView.setMyName(name: String?) {
this.text =
if (name.isNullOrEmpty()) "" else "${this.context.getString(R.string.Generic_Text)} $name"
}
그런 다음 android:text
속성 대신 XML에서 바인딩 어댑터를 사용하십시오.
app:my_name="@{Profile.name}"
답변
2019 업데이트, Android 스튜디오를 3.4로, Android Gradle 플러그인을 3.4로
더 이상 가져올 필요가 없습니다.
<import type="java.lang.String" />"
문자열 연산을 위해. 이 답변을 확인하십시오 .
답변
끝에 포함 할 리소스 문자열을 변경할 수없는 경우 %s
(예 : 접미사없이 다른 곳에서 사용되기 때문에) :
android:text="@{@string/Generic_Text.concat(Profile.name)}"
Profile.name
null이 될 수 없다면 충분합니다. 그러나 일이 null
발생 하면 충돌합니다. 다른 레이어를 추가해야합니다.
android:text="@{@string/Generic_Text.concat(Objects.toString(Profile.name))}"
( <import type="java.util.Objects"/>
작동 해야 합니다.)
다시 말하지만,이 모든 추가 작업은 리소스 문자열이 다른 곳에서 사용되는 경우에만 가치가 있습니다. 두 번째 이유는 null
“null”리터럴 대신 “빈 문자열” 로 처리하려는 경우 입니다.
답변
XML로 텍스트 를 입력 하려는 경우 ” 인용문을 사용할 수 있습니다 .
android:text="@{`Device Name`}"
다른 곳 에서 String 또는 변수와 연결 해야 할 경우 사용할 수 있습니다.
android:text="@{`Device Name`.concat(android.os.Build.MANUFACTURER)}"
할 수있는 변수 대신 문자열 리소스 를 연결 하려면
android:text="@{@string/app_name.concat(`Device Name`)}"