Android 样式布局

一、样式布局

  首先,先看下面这段样式布局代码,这里称在xml控件上添加属性为内联(仅限于本篇博文这样称呼):

<Button
    android:id="@+id/crime_date"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"/>

<CheckBox
    android:id="@+id/crime_solved"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:text="@string/solved_btn"/>

<Button
    android:id="@+id/choose_btn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:text="@string/choose_name"/>

<Button
    android:id="@+id/contact_btn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:text="@string/contacter_name"/>

  在上面这段XML布局代码,可以看出,各个按钮的样式完全一样,添加某个属性给控件就有多次。假如,有更多的控件使用相同的而已,要做N次重复的工作。Android提供了各种样式UI,可用于解决重复性的工作。样式资源类似于CSS样式。样式也可以具有层级结构:子样式拥有父式样同样的属性及属性值,可以覆盖它们,也可以添加新的属性。

  类似字符资源,样式定义在XML文件的<Resources>酬标签内,并存放在res/values目录中。另外,资源文件取什么名并不重要,但根据约定,样式通常定义在style.xml文件中。在Android项目中,已经默认创建了这个文件。

  Style.xml文件:

1 <resources>
2     <style name="Crime_btn_style1">
3         <item name="android:layout_width">wrap_content</item>
4         <item name="android:layout_height">wrap_content</item>
5         <item name="android:layout_marginLeft">16dp</item>
6         <item name="android:layout_marginRight">16dp</item>
7     </style>
8 </resources>

  layout/fragment_crime.xml文件,将控件的属性以style样式名引入的方式在外联(仅限于本篇博文这样称呼):

 1 <Button
 2     android:id="@+id/crime_date"
 3     style="@style/Crime_btn_style1"/>
 4
 5 <CheckBox
 6     android:id="@+id/crime_solved"
 7     android:text="@string/solved_btn"
 8     style="@style/Crime_btn_style1"/>
 9
10 <Button
11     android:id="@+id/choose_btn"
12     android:text="@string/choose_name"
13     style="@style/Crime_btn_style1"/>
14
15 <Button
16     android:id="@+id/contact_btn"
17     android:text="@string/contacter_name"
18     style="@style/Crime_btn_style1"/>

PS:需要注意的是,在内联属性与外联属性冲突时,以内联属性为准,也可以说,内联属性优于外联属性方式。

二、include与merge

  include使用资源ID引入而已文件。

  layout/button_row.xml

1 <TableRow
2 xmlns:android="http:/schemas.android.com/apk/res/android">
3     <Button style="@style/Crime_btn_style1">
4     <Button style="@style/Crime_btn_style1">
5     <Button style="@style/Crime_btn_style1">
6 </TableRow>

  layout/fragment_crime.xml

1 <include
2     android:layout_weight="1"
3     layout="@layout/button_row" />
4 <include
5     android:layout_weight="1"
6     layout="@layout/button_row" />
7 <include
8     android:layout_weight="1"
9     layout="@layout/button_row" />

以上代码表明,include将引入资源ID为button_row文件内容。

PS:include标签只能引用layout布局文件,也就是只能引用layout目录下的布局文件。

时间: 2024-08-06 08:21:03

Android 样式布局的相关文章

android样式布局---&amp;gt;ListView(附上源代码)

在android应用开发过程中,Listview 是经常使用的数据展现控件,往往用于显示列表形式的数据. 假设只显示数据往往会显得非常单调.非常多时候依据须要定义不同的item 背景选项.比如定义数据的标题.表头或者间隔显示Listview item背景内容. 如图的所看到的的背景内容,如今以一个WeatherDemo为模型分析Listview的相关用法. 实现思路:定义Listview不同的背景,首先要定义好标题与表格内容的两个不同的xml布局文件(city_item.xml,content_

Android 样式 (style) 和主题(theme)

转载:https://gold.xitu.io/post/58441c48c59e0d0056a30bc2 样式和主题 样式是指为 View 或窗口指定外观和格式的属性集合.样式可以指定高度.填充.字体颜色.字号.背景色等许多属性. 样式是在与指定布局的 XML 不同的 XML 资源中进行定义. Android 中的样式与网页设计中层叠样式表的原理类似 - 您可以通过它将设计与内容分离. 例如,通过使用样式,您可以将以下布局 XML: <TextView android:layout_width

[转]Android样式的开发:shape篇

转载自Keegan小钢原文链接:http://keeganlee.me/post/android/20150830 Android样式的开发:shape篇Android样式的开发:selector篇Android样式的开发:layer-list篇Android样式的开发:drawable汇总篇Android样式的开发:View Animation篇Android样式的开发:Property Animation篇Android样式的开发:Style篇 一个应用,应该保持一套统一的样式,包括Butt

[转]Android:布局实例之模仿QQ登录界面

Android:布局实例之模仿QQ登录界面 预览图: 准备: 1.找到模仿对象 QQ登陆界面UI下载>>>>> 2.导入工程 3.查看布局结构和使用控件 其对应效果图分布为 4.分析样式选择器 下拉箭头2种样式:点击和默认状态 文本框2种样式:聚焦和默认状态 复选框3种样式:选择.不选择和鼠标点着不放 左下角按钮2种样式:点击和默认 登录按钮2样式:点击和默认 ============================================帖代码===========

android layout布局属性

参考:http://blog.csdn.net/msmile_my/article/details/9018775 第一类:属性值 true或者 false           android:layout_centerHrizontal 水平居中     android:layout_centerVertical 垂直居中     android:layout_centerInparent 相对于父元素完全居中     android:layout_alignParentBottom 贴紧父元

Android 样式和主题(style &amp; theme)

Android 样式 android中的样式和CSS样式作用相似,都是用于为界面元素定义显示风格,它是一个包含一个或者多个view控件属性的集合.如:需要定义字体的颜色和大小. 在CSS中是这样定义的: <style> .wu{COLOR:#0000CC;font-size:18px;} </style> 可以像这样使用上面的css样式:<div class="wu">wuyudong‘blog</div> 在Android中可以这样定义

Android:布局实例之常见用户设置界面

实现效果: 整理思路: 1.控件:文字TextView 和 右箭头ImageView 2.因为考虑到点击效果,设计为:最外层为全圆角,内层有四种情况,分别为上圆角.无圆角.下圆角和全圆角. 3.内层样式效果:需要初始样式.和点击样式 4.需要知识:结合style.shake.selector组合样式 布局: 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android=

[转]android layout布局属性

参考:http://blog.csdn.net/msmile_my/article/details/9018775 第一类:属性值 true或者 false           android:layout_centerHrizontal 水平居中     android:layout_centerVertical 垂直居中     android:layout_centerInparent 相对于父元素完全居中     android:layout_alignParentBottom 贴紧父元

Android样式的开发:shape篇

转载请注明:转载自Keegan小钢并标明原文链接:http://keeganlee.me/post/android/20150830微信订阅号:keeganlee_me写于2015-08-30 Android样式的开发:shape篇Android样式的开发:selector篇Android样式的开发:layer-list篇Android样式的开发:drawable汇总篇Android样式的开发:View Animation篇Android样式的开发:Property Animation篇Andr