网站互动功能,郑州网站建设饣汉狮网络,揭秘低价网站建设危害,公众号如何推广第一部分 安卓开发中#xff0c;在写布局代码的时候#xff0c;ide可以看到布局的预览效果。 但是有些效果则必须在运行之后才能看见#xff0c;比如这种情况#xff1a;TextView在xml中没有设置任何字符#xff0c;而是在activity中设置了text。因此为了在ide中预览效果在写布局代码的时候ide可以看到布局的预览效果。 但是有些效果则必须在运行之后才能看见比如这种情况TextView在xml中没有设置任何字符而是在activity中设置了text。因此为了在ide中预览效果你必须在xml中为TextView控件设置android:text属性 TextViewandroid:idid/text_mainandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:textAppearancestyle/TextAppearance.Titleandroid:layout_margindimen/main_marginandroid:textI am a title / 一般我们在这样做的时候都告诉自己没关系等写完代码我就把这些东西一并删了。但是你可能会忘以至于在你的最终产品中也会有这样的代码。 用tools吧别做傻事 以上的情况是可以避免的我们使用tools命名空间以及其属性来解决这个问题。 xmlns:toolshttp://schemas.android.com/tools tools可以告诉Android Studio哪些属性在运行的时候是被忽略的只在设计布局的时候有效。比如我们要让android:text属性只在布局预览中有效可以这样 TextViewandroid:idid/text_mainandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:textAppearancestyle/TextAppearance.Titleandroid:layout_margindimen/main_margintools:textI am a title / tools可以覆盖android的所有标准属性将android:换成tools:即可。同时在运行的时候就连tools:本身都是被忽略的不会被带进apk中。 tools属性的种类 tools属性可以分为两种一种是影响Lint提示的一种是关于xml布局设计的。以上介绍的是tools的最基本用法在UI设计的时候覆盖标准的android属性属于第二种。下面介绍Lint相关的属性。 Lint相关的属性 tools:ignoretools:targetApitools:locale tools:ignore ignore属性是告诉Lint忽略xml中的某些警告。 假设我们有这样的一个ImageView ImageViewandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginStartdimen/margin_mainandroid:layout_marginTopdimen/margin_mainandroid:scaleTypecenterandroid:srcdrawable/divider / Lint会提示该ImageView缺少android:contentDescription属性。我们可以使用tools:ignore来忽略这个警告 ImageViewandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginStartdimen/margin_mainandroid:layout_marginTopdimen/margin_mainandroid:scaleTypecenterandroid:srcdrawable/dividertools:ignorecontentDescription / tools:targetApi 假设minSdkLevel 15而你使用了api21中的控件比如RippleDrawable ripple xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:colorcolor/accent_color / 则Lint会提示警告。 为了不显示这个警告可以 ripple xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:toolshttp://schemas.android.com/toolsandroid:colorcolor/accent_colortools:targetApiLOLLIPOP / tools:locale本地语言属性 默认情况下res/values/strings.xml中的字符串会执行拼写检查如果不是英语会提示拼写错误通过以下代码来告诉studio本地语言不是英语就不会有提示了。 resourcesxmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:toolshttp://schemas.android.com/toolstools:localeit!-- Your strings go here --/resources 这篇文章首先介绍了tools的最基本用法-覆盖android的属性然后介绍了忽略Lint提示的属性。下篇文章中我们将继续介绍关于UI预览的其他属性非android标准属性。 ps关于忽略Lint的属性如果不想了解的话也没关系因为并不影响编译一般我都不会管这些警告。 第二部分 这部分我们将继续介绍关于UI预览的其他属性非android标准属性。 tools:context tools:menu tools:actionBarNavMode tools:listitem/listheader/listfooter tools:showIn tools:layout tools:context context属性其实正是的称呼是activity属性有了这个属性ide就知道在预览布局的时候该采用什么样的主题。同时他还可以在android studio的java代码中帮助找到相关的文件Go to Related files 该属性的值是activity的完整包名 LinearLayoutxmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:toolshttp://schemas.android.com/toolsandroid:idid/containerandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:orientationverticaltools:contextcom.android.example.MainActivity !-- ... --/LinearLayout tools:menu 告诉IDE 在预览窗口中使用哪个菜单这个菜单将显示在layout的根节点上actionbar的位置。 其实预览窗口非常智能如果布局和一个activity关联指上面所讲的用tools:context关联它将会自动查询相关activity的onCreateOptionsMenu方法中的代码以显示菜单。而menu属性则可以覆盖这种默认的行为。 你还可以为menu属性定义多个菜单资源不同的菜单资源之间用逗号隔开。 tools:menumenu_main,menu_edit 如果你不希望在预览图中显示菜单则 tools:menu 最后需要注意当主题为Theme.AppCompat时这个属性不起作用。 tools:actionBarNavMode 这个属性告诉ide app barMaterial中对actionbar的称呼的显示模式其值可以是 standardtabslist LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:toolshttp://schemas.android.com/toolsandroid:orientationverticalandroid:layout_widthmatch_parentandroid:layout_heightmatch_parenttools:actionBarNavModetabs / 同样的当主题是Theme.AppCompat (r21, at least) 或者Theme.Material,或者使用了布局包含Toolbar的方式。 该属性也不起作用只有holo主题才有效。 listitem, listheader 和listfooter 属性 顾名思义就是在ListView ExpandableListView等的预览效果中添加头部 尾部 以及子item的预览布局。 GridViewandroid:idid/listandroid:layout_widthmatch_parentandroid:layout_heightwrap_contenttools:listheaderlayout/list_headertools:listitemlayout/list_itemtools:listfooterlayout/list_footer / layout属性 tools:layout告诉ideFragment在程序预览的时候该显示成什么样 fragment xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:toolshttp://schemas.android.com/toolsandroid:idid/item_listandroid:namecom.example.fragmenttwopanel.ItemListFragmentandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:layout_marginLeft16dpandroid:layout_marginRight16dptools:layoutandroid:layout/list_content / tools:showIn 该属性设置于一个被其他布局include的布局的根元素上。这让您可以指向包含此布局的其中一个布局在设计时这个被包含的布局会带着周围的外部布局被渲染。这将允许您“在上下文中”查看和编辑这个布局。需要 Studio 0.5.8 或更高版本。转载于:https://www.cnblogs.com/to-creat/p/5704367.html