programing

Android strings.xml에 문자 &을 쓰는 방법

css3 2023. 4. 10. 22:06

Android strings.xml에 문자 &을 쓰는 방법

나는 다음과 같이 적었다.strings.xml파일:

<string name="game_settings_dragNDropMove_checkBox">Move by Drag&Drop</string>

다음 오류가 발생하였습니다.

The reference to entity "Drop" must end with the ';' delimiter.

strings.xml에 문자 &을 쓰는 방법은 무엇입니까?

인코딩:

&amp;

특수 문자의 경우 보통 유니코드 정의를 사용합니다. 예를 들어 다음과 같습니다.맞는 경우 \u0026.여기 좋은 레퍼런스 페이지가 있습니다.http://jrgraphix.net/research/unicode_blocks.php?block=0

이것은 나의 문제이며, 해결 방법은 다음과 같습니다.사용하다&gt;위해서>,&lt;위해서<,&amp;위해서&,"'"위해서',&quot위해서\"\"

당신의 질문조차 답이 되었지만, 여전히 나는 더 많은 사람들에게 이와 같이 말하고 싶다.이것들은html entitiesAndroid에서는 다음과 같이 씁니다.

다음 내용으로 대체:

& with &amp;
> with &gt;
< with &lt;
" with &quot;, &ldquo; or &rdquo;
' with &apos;, &lsquo; or &rsquo;
} with &#125;

다음과 같이 해야 합니다.

<string name="game_settings_dragNDropMove_checkBox">Move by Drag&amp;Drop</string>

다음과 같은 문자열(&)

<string name="string_abc">Translate &amp; Scan Objects</string>

출력은 다음과 같습니다.

오브젝트 번역 및 스캔

Android Studio에서처럼 문자열의 내용을 XML CDATA에 저장할 수도 있습니다.Extract string resource

<string name="game_settings_dragNDropMove_checkBox"><![CDATA[Move by Drag&Drop]]></string>

이것은 매우 오래된 것일 수 있습니다.하지만 빠른 코드를 찾는 사람들을 위해.

 public String handleEscapeCharacter( String str ) {
    String[] escapeCharacters = { "&gt;", "&lt;", "&amp;", "&quot;", "&apos;" };
    String[] onReadableCharacter = {">", "<", "&", "\"\"", "'"};
    for (int i = 0; i < escapeCharacters.length; i++) {
        str = str.replace(escapeCharacters[i], onReadableCharacter[i]);
    } return str;
 }

이스케이프 문자를 처리하는 경우 해당 배열에 문자와 기호를 추가할 수 있습니다.

-응원

오류를 방지하려면 extract 문자열을 사용합니다.

<string name="travels_tours_pvt_ltd"><![CDATA[Travels & Tours (Pvt) Ltd.]]></string>

이렇게 쓸 수 있어요.

<string name="you_me">You &#38; Me<string>

출력: 너와 나

Mac 및 Android 스튜디오 사용자:

string.xml 또는 레이아웃에 & 등의 문자를 입력하고 "Option" 및 "return" 키를 선택합니다.스크린샷을 참조해 주세요.

문자열을 xml로 직접 하드코드하는 경우 이 방법으로 추가할 수 있습니다.

  <TextView 
     android:layout_marginStart="5dp"
     android:layout_marginLeft="5dp"
     android:layout_centerVertical="true"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="You &amp; Me"

제가 찾은 가장 간단한 방법은 &을 유니코드 대응 문자로 바꾸는 것입니다.이 예에서는, 다음과 같이 기입합니다.

<string name="game_settings_dragNDropMove_checkBox">Move by Drag\u0026Drop</string>

위의 인코딩 중 어느 것도 저에게 효과가 없었기 때문에 글을 올립니다.

앰퍼샌드를 시퀀스로 대체하면&#38;는 xml 문자열 리소스로 동작했습니다.예:

<string name="webLink"><a href="https://www.example.com?param1=option1&#38;param2=option2">click here</a></string>

언급URL : https://stackoverflow.com/questions/3053062/how-to-write-character-in-android-strings-xml