欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

深入理解Android中的xmlns:tools屬性

 更新時間:2016年12月14日 14:28:51   作者:Andye  
關(guān)于xmlns:tools屬性的介紹網(wǎng)上有很多,小編覺得有必要整理一篇介紹較為詳細的內(nèi)容給大家,下面這篇文章就很深入的介紹了關(guān)于Android中的xmlns:tools屬性,有需要的朋友們可以參考借鑒,下面來一起看看吧。

前言

安卓開發(fā)中,在寫布局代碼的時候,ide可以看到布局的預(yù)覽效果。

但是有些效果則必須在運行之后才能看見,比如這種情況:TextView在xml中沒有設(shè)置任何字符,而是在activity中設(shè)置了text。因此為了在ide中預(yù)覽效果,你必須在xml中為TextView控件設(shè)置android:text屬性

<TextView

android:id="@+id/text_main"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textAppearance="@style/TextAppearance.Title"

android:layout_margin="@dimen/main_margin"

android:text="I am a title" />

一般我們在這樣做的時候都告訴自己,沒關(guān)系,等寫完代碼我就把這些東西一并刪了。但是你可能會忘,以至于在你的最終產(chǎn)品中也會有這樣的代碼。

用tools吧,別做傻事

以上的情況是可以避免的,我們使用tools命名空間以及其屬性來解決這個問題。

tools可以告訴Android Studio,哪些屬性在運行的時候是被忽略的,只在設(shè)計布局的時候有效。比如我們要讓android:text屬性只在布局預(yù)覽中有效可以這樣

<TextView

android:id="@+id/text_main"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textAppearance="@style/TextAppearance.Title"

android:layout_margin="@dimen/main_margin"

tools:text="I am a title" />

tools可以覆蓋android的所有標(biāo)準(zhǔn)屬性,將android:換成tools:即可。同時在運行的時候就連tools:本身都是被忽略的,不會被帶進apk中。

tools屬性的種類

tools屬性可以分為兩種:一種是影響Lint提示的,一種是關(guān)于xml布局設(shè)計的。以上介紹的是tools的最基本用法:在UI設(shè)計的時候覆蓋標(biāo)準(zhǔn)的android屬性,屬于第二種。下面介紹Lint相關(guān)的屬性。

Lint相關(guān)的屬性

tools:ignore

tools:targetApi

tools:locale

tools:ignore

ignore屬性是告訴Lint忽略xml中的某些警告。

假設(shè)我們有這樣的一個ImageView

<ImageView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginStart="@dimen/margin_main"

android:layout_marginTop="@dimen/margin_main"

android:scaleType="center"

android:src="@drawable/divider" />

Lint會提示該ImageView缺少android:contentDescription屬性。我們可以使用tools:ignore來忽略這個警告:

<ImageView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginStart="@dimen/margin_main"

android:layout_marginTop="@dimen/margin_main"

android:scaleType="center"

android:src="@drawable/divider"

tools:ignore="contentDescription" />

tools:targetApi

假設(shè)minSdkLevel 15,而你使用了api21中的控件比如RippleDrawable

<ripple xmlns:android="http://schemas.android.com/apk/res/android"

android:color="@color/accent_color" />

則Lint會提示警告。

為了不顯示這個警告,可以:

<ripple xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:color="@color/accent_color"

tools:targetApi="LOLLIPOP" />

tools:locale(本地語言)屬性

默認(rèn)情況下res/values/strings.xml中的字符串會執(zhí)行拼寫檢查,如果不是英語,會提示拼寫錯誤,通過以下代碼來告訴studio本地語言不是英語,就不會有提示了。

<resources

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

tools:locale="it">


<!-- Your strings go here -->


</resources>

上面首先介紹了tools的最基本用法-覆蓋android的屬性,然后介紹了忽略Lint提示的屬性。下面我們將繼續(xù)介紹關(guān)于UI預(yù)覽的其他屬性(非android標(biāo)準(zhǔn)屬性)。

注意:關(guān)于忽略Lint的屬性,如果不想了解的話也沒關(guān)系,因為并不影響編譯,一般我都不會管這些警告。

這部分我們將繼續(xù)介紹關(guān)于UI預(yù)覽的其他屬性(非android標(biāo)準(zhǔn)屬性)。

  1. tools:context
  2. tools:menu
  3. tools:actionBarNavMode
  4. tools:listitem/listheader/listfooter
  5. tools:showIn
  6. tools:layout

tools:context

context屬性其實正是的稱呼是activity屬性,有了這個屬性,ide就知道在預(yù)覽布局的時候該采用什么樣的主題。同時他還可以在android studio的java代碼中幫助找到相關(guān)的文件(Go to Related files)

該屬性的值是activity的完整包名

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/container"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context="com.android.example.MainActivity"> <!-- ... -->

</LinearLayout>

tools:menu

告訴IDE 在預(yù)覽窗口中使用哪個菜單,這個菜單將顯示在layout的根節(jié)點上(actionbar的位置)。

其實預(yù)覽窗口非常智能,如果布局和一個activity關(guān)聯(lián)(指上面所講的用tools:context關(guān)聯(lián))它將會自動查詢相關(guān)activity的onCreateOptionsMenu方法中的代碼,以顯示菜單。而menu屬性則可以覆蓋這種默認(rèn)的行為。

你還可以為menu屬性定義多個菜單資源,不同的菜單資源之間用逗號隔開。

tools:menu="menu_main,menu_edit"

如果你不希望在預(yù)覽圖中顯示菜單則:

tools:menu=""

最后需要注意,當(dāng)主題為Theme.AppCompat時,這個屬性不起作用。

tools:actionBarNavMode

這個屬性告訴ide  app bar(Material中對actionbar的稱呼)的顯示模式,其值可以是

standard

tabs

list
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:actionBarNavMode="tabs" />

同樣的,當(dāng)主題是Theme.AppCompat (r21+, at least) 或者Theme.Material,或者使用了布局包含Toolbar的方式。  該屬性也不起作用,只有holo主題才有效。

listitem, listheader 和listfooter 屬性

顧名思義就是在ListView ExpandableListView等的預(yù)覽效果中添加頭部 尾部 以及子item的預(yù)覽布局。

<GridView

android:id="@+id/list"

android:layout_width="match_parent"

android:layout_height="wrap_content"

tools:listheader="@layout/list_header"

tools:listitem="@layout/list_item"

tools:listfooter="@layout/list_footer" />

layout屬性

tools:layout告訴ide,F(xiàn)ragment在程序預(yù)覽的時候該顯示成什么樣

<fragment xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/item_list"

android:name="com.example.fragmenttwopanel.ItemListFragment"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_marginLeft="16dp"

android:layout_marginRight="16dp"

tools:layout="@android:layout/list_content" />

tools:showIn

該屬性設(shè)置于一個被其他布局<include>的布局的根元素上。這讓您可以指向包含此布局的其中一個布局,在設(shè)計時這個被包含的布局會帶著周圍的外部布局被渲染。這將允許您“在上下文中”查看和編輯這個布局。需要 Studio 0.5.8 或更高版本。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。

相關(guān)文章

  • Android監(jiān)聽鍵盤狀態(tài)獲取鍵盤高度的實現(xiàn)方法

    Android監(jiān)聽鍵盤狀態(tài)獲取鍵盤高度的實現(xiàn)方法

    這篇文章主要給大家介紹了關(guān)于Android監(jiān)聽鍵盤狀態(tài)獲取鍵盤高度的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • flutter?text組件使用示例詳解

    flutter?text組件使用示例詳解

    這篇文章主要為大家介紹了flutter?text組件使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • Android中的binder機制詳解

    Android中的binder機制詳解

    這篇文章主要介紹了Android中的binder機制詳解,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下
    2021-04-04
  • 關(guān)于Android Activity之間傳遞數(shù)據(jù)的6種方式

    關(guān)于Android Activity之間傳遞數(shù)據(jù)的6種方式

    這篇文章主要介紹了關(guān)于Android Activity之間傳遞數(shù)據(jù)的6種方式,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-03-03
  • Android操作SQLite基本用法

    Android操作SQLite基本用法

    這篇文章主要介紹了Android操作SQLite基本用法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2021-12-12
  • Android 獲取手機信息實例詳解

    Android 獲取手機信息實例詳解

    這篇文章主要介紹了Android 獲取手機信息實例詳解的相關(guān)資料,這里附有實例代碼及實現(xiàn)效果圖,需要的朋友可以參考下
    2017-01-01
  • Android開發(fā)中那些需要注意的坑

    Android開發(fā)中那些需要注意的坑

    這篇文章主要介紹了Android開發(fā)過程中那些需要注意的坑,有一些是自己遇到的,特分享給大家,需要的朋友可以參考下
    2016-04-04
  • 靈活使用Android中ActionBar和ViewPager切換頁面

    靈活使用Android中ActionBar和ViewPager切換頁面

    這篇文章主要介紹了如何靈活使用Android中ActionBar和ViewPager切換頁面,感興趣的小伙伴們可以參考一下
    2015-12-12
  • Android中TextView顯示圓圈背景或設(shè)置圓角的方法

    Android中TextView顯示圓圈背景或設(shè)置圓角的方法

    TextView顯示文本給用戶,并允許他們選擇編輯。TextView是一個完整的文本編輯器,但是其基本類配置為不允許編輯。下面這篇文章主要給大家介紹了關(guān)于Android中TextView顯示圓圈背景或設(shè)置圓角的方法,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-05-05
  • 詳解Android aidl的使用方法

    詳解Android aidl的使用方法

    AIDL是Android中IPC(Inter-Process Communication)方式中的一種,AIDL是Android Interface definition language的縮寫。這篇文章主要介紹了Android aidl的使用方法,感興趣的朋友跟隨小編一起看看吧
    2020-07-07

最新評論