Android Studio Tips -- 布局預覽

jopen 9年前發布 | 32K 次閱讀 Android開發 移動開發 Android Studio

轉載自:http://stormzhang.com/devtools/2015/01/11/android-studio-tips1/

我們都知道Android Studio用起來很棒,其中布局預覽更棒。我們在調UI的時候基本是需要實時預覽來看效果的,在Android Studio中只需要切換到Design就可以看到,而且我們需要在布局上填充數據預覽效果更好,比如我們在TextView中設定text屬性來看下字 體大小與布局是否正確,但是呢正式環境我們又需要移除這些額外的數據,不然看著很不舒服,這個時候就用到了本篇博客介紹的一個技巧。

廢話不多說,直接上圖:

Android Studio Tips -- 布局預覽

上述示例中只需要在xml布局文件中添加tools命名空間的text屬性就ok了,此屬性和正式發布的版本完全無關,是不是很酷?

用之前只需要在跟布局添加命名空間就ok了

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    ...

用法很簡單,只需要用tools的命名空間代替android的命名空間,我們可以這樣使用
<ListView
    android:id="@+id/listView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fastScrollAlwaysVisible="true"
    tools:fastScrollAlwaysVisible=""/>

也可以這樣使用
<Button
  android:id="@+id/button"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="First"
  tools:visibility="invisible" />
<Button
  android:id="@+id/button2"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Second"
  tools:visibility="visible" />

注意的是tools屬性只能在layout文件中使用,而且只能使用framework自帶的一些屬性,不可以用使用自定義屬性,不過這足夠了,基本上能滿足我們的需求了。

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!