Android Studio如何輕鬆整理字串到string.xml中

來源:互聯網
上載者:User

標籤:

1、在布局檔案中寫的字串,整理到string.xml中

1、如下面的布局檔案,一般寫布局檔案的時候都是直接把字串寫到裡面,之後再整理到string.xml中:

<TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="測試Alt+Enter"        android:maxLines="1"        android:textSize="36sp" />


這個時候會有提示,要將該字串整理到string.xml中,為了方便I18N國際化處理,如所示: 

2、然後選中字串“測試Alt+Enter”,同時按住Alt+Enter,即可看到彈出選擇的對話款,選擇其中的“Extract string resource”,如所示: 

選擇其中的“Extract string resource”,出現如所示的對話方塊 

然後可以修改該字串在string.xml中的key值,如所示: 

修改完後,點擊“OK”按鈕,就會自動修改代碼為如下所示的代碼:

   <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/test_alt_enter"        android:maxLines="1"        android:textSize="36sp" />

同時,在string.xml檔案中也會自動產生相應的代碼,如所示:

<string name="test_alt_enter">測試Alt+Enter</string>

2、在Java檔案中寫的字串,整理到string.xml中

和在布局檔案中寫的字串,整理到string.xml中的方式是一樣的,也是使用Alt + Enter快速鍵進行處理,如所示:

tvNavChannelTitle.setText("在Java檔案中測試Alt+Enter");

使用Alt + Enter快速鍵,然後彈出如下的對話方塊 

選擇其中的“Extract string resource”, 然後彈出如所示的對話方塊,然後修改該字串在string.xml中的key值 

點擊“OK”按鈕,然後就會自動修改代碼為如下所示的代碼:

 tvNavChannelTitle.setText(R.string.test_alt_enter_java);

同時,在string.xml檔案中也會自動產生相應的代碼,如所示:

<string name="test_alt_enter_java">在Java檔案中測試Alt+Enter</string>

 

總結:在需要整理String的地方,按下Alt+Enter進行代碼提示修改

 

原文出處:http://blog.csdn.net/ouyang_peng/article/details/52280656

 

Android Studio如何輕鬆整理字串到string.xml中

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.