`
cloklo
  • 浏览: 28280 次
  • 性别: Icon_minigender_1
  • 来自: 襄阳
社区版块
存档分类
最新评论

Android 用style简化layout布局文件

 
阅读更多

我有一个页面上面有若干个button样式都是相同的 在屏幕居中 25号白色字

  之前的代码如下:

  Xml代码 收藏代码

  1. < ?xml version="1.0" encoding="utf-8"?>

  2. < RelativeLayout

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

  4. android:layout_width="fill_parent"

  5. android:layout_height="fill_parent"

  6. android:background="@color/background">

  7. < ImageView

  8. android:id="@+id/home_iv1"

  9. android:layout_width="wrap_content"

  10. android:layout_height="wrap_content"

  11. android:paddingTop="15dip"

  12. android:layout_centerHorizontal="true"

  13. android:src="@drawable/home"

  14. />

  15. < Button

  16. android:id="@+id/home_bt1"

  17. android:layout_width="wrap_content"

  18. android:layout_height="wrap_content"

  19. android:layout_centerHorizontal="true"

  20. android:layout_below="@id/home_iv1"

  21. android:layout_marginTop="15dip"

  22. android:background="@drawable/selector"

  23. android:gravity="center"

  24. android:text="@string/home_b1"

  25. />

  26. ........

  27. < Button

  28. android:id="@+id/home_bt5"

  29. android:layout_width="wrap_content"

  30. android:layout_height="wrap_content"

  31. android:layout_centerHorizontal="true"

  32. android:layout_below="@id/home_bt4"

  33. android:layout_marginTop="15dip"

  34. android:background="@drawable/selector"

  35. android:gravity="center"

  36. android:text="@string/home_b5"

  37. />

  38. < /RelativeLayout>

  < ?xml version="1.0" encoding="utf-8"?>

  < RelativeLayout

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

 android:layout_width="fill_parent"

  android:layout_height="fill_parent"

  android:background="@color/background">

  < ImageView

  android:id="@+id/home_iv1"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content"

  android:paddingTop="15dip"

  android:layout_centerHorizontal="true"

  android:src="@drawable/home"

  />

  < Button

  android:id="@+id/home_bt1"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content"

  android:layout_centerHorizontal="true"

  android:layout_below="@id/home_iv1"

  android:layout_marginTop="15dip"

  android:background="@drawable/selector"

  android:gravity="center"

  android:text="@string/home_b1"

  />

  ........

  < Button

  android:id="@+id/home_bt5"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content"

  android:layout_centerHorizontal="true"

  android:layout_below="@id/home_bt4"

  android:layout_marginTop="15dip"

  android:background="@drawable/selector"

  android:gravity="center"

  android:text="@string/home_b5"

  />

  < /RelativeLayout>

  我们可以看到这五个按钮除了ID和text、还有below(因为用的相对布局,如果用线性布局这块也可以相同)其他的都一样,那我们能不能简化一下这些重复的代码呢?可以,用style。

  先看看用style怎么设置这些重复的属性:

  在values下新建style.xml

  Xml代码 收藏代码

   < ?xml version="1.0" encoding="utf-8"?>

   < resources>

   < style name="homebtn" >

   < item name="android:layout_width">wrap_content< /item>

   < item name="android:layout_height">wrap_content< /item>

     < item name="android:textSize">20sp< /item>

   < item name="android:textColor">#FFFFffff< /item>

   < item name="android:gravity">center< /item>

   < item name="android:layout_marginTop">15dip< /item>

   < item name="android:layout_centerHorizontal">true< /item>

   < item name="android:background">@drawable/selector< /item>

   < /style>

   < /resources>

  < ?xml version="1.0" encoding="utf-8"?>

  < resources>

  < style name="homebtn" >

  < item name="android:layout_width">wrap_content< /item>

  < item name="android:layout_height">wrap_content< /item>

  < item name="android:textSize">20sp< /item>

  < item name="android:textColor">#FFFFffff< /item>

  < item name="android:gravity">center< /item>

  < item name="android:layout_marginTop">15dip< /item>

  < item name="android:layout_centerHorizontal">true< /item>

  < item name="android:background">@drawable/selector< /item>

  < /style>

  < /resources>

  在回到我们的布局文件在按钮中设置style="@style/homebtn"就可以了

  Xml代码 收藏代码

   < ?xml version="1.0" encoding="utf-8"?>

   < RelativeLayout

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

   android:layout_width="fill_parent"

   android:layout_height="fill_parent"

   android:background="@color/background">

   < ImageView

   android:id="@+id/home_iv1"

   android:layout_width="wrap_content"

   android:layout_height="wrap_content"

   android:paddingTop="15dip"

  android:layout_centerHorizontal="true"

   android:src="@drawable/home"

   />

   < Button

   android:id="@+id/home_bt1"

   style="@style/homebtn"

   android:layout_below="@id/home_iv1"

   android:text="@string/home_b1"

   />

   .......

   < Button

   android:id="@+id/home_bt5"

   style="@style/homebtn"

   android:layout_below="@id/home_bt4"

  android:text="@string/home_b5"

   />

  < /RelativeLayout>

  < ?xml version="1.0" encoding="utf-8"?>

  < RelativeLayout

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

  android:layout_width="fill_parent"

  android:layout_height="fill_parent"

  android:background="@color/background">

  < ImageView

  android:id="@+id/home_iv1"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content"

  android:paddingTop="15dip"

  android:layout_centerHorizontal="true"

  android:src="@drawable/home"

  />

  < Button

  android:id="@+id/home_bt1"

  style="@style/homebtn"

  android:layout_below="@id/home_iv1"

  android:text="@string/home_b1"

  />

  .......

  < Button

  android:id="@+id/home_bt5"

  style="@style/homebtn"

  android:layout_below="@id/home_bt4"

  android:text="@string/home_b5"

  />

  < /RelativeLayout>

  其实在style中可以定义多个样式给不同的组件使用,现在我们把ImageView也用样式定义一下吧:

  Xml代码 收藏代码

  < ?xml version="1.0" encoding="utf-8"?>

  < resources>

  < style name="homebtn" >

  < item name="android:layout_width">wrap_content< /item>

  < item name="android:layout_height">wrap_content< /item>

  < item name="android:textSize">20sp< /item>

  < item name="android:textColor">#FFFFffff< /item>

  < item name="android:gravity">center< /item>

  < item name="android:layout_marginTop">15dip< /item>

  < item name="android:layout_centerHorizontal">true< /item>

  < item name="android:background">@drawable/selector< /item>

  < /style>

  < style name="homeiv">

  < item name="android:layout_width">wrap_content< /item>

  < item name="android:layout_height">wrap_content< /item>

  < item name="android:layout_marginTop">15dip< /item>

  < item name="android:layout_centerHorizontal">true< /item>

  < /style>

  < /resources>

  < ?xml version="1.0" encoding="utf-8"?>

  < resources>

  < style name="homebtn" >

  < item name="android:layout_width">wrap_content< /item>

  < item name="android:layout_height">wrap_content< /item>

  < item name="android:textSize">20sp< /item>

  < item name="android:textColor">#FFFFffff< /item>

  < item name="android:gravity">center< /item>

  < item name="android:layout_marginTop">15dip< /item>

  < item name="android:layout_centerHorizontal">true< /item>

  < item name="android:background">@drawable/selector< /item>

  < /style>

  < style name="homeiv">

  < item name="android:layout_width">wrap_content< /item>

       < item name="android:layout_height">wrap_content< /item>

  < item name="android:layout_marginTop">15dip< /item>

  < item name="android:layout_centerHorizontal">true< /item>

  < /style>

  < /resources>

  好了介绍到这里也就完了。等。。等一下这里好像出现了和刚才的布局文件一样的问题,重复的代码!(我是不是有重复癖,看见重复就想消灭。。)

  这里的重复能不能消灭呢,这也是可以的。在style下还有一个个有用的属性:

  parent ? 可选,一些在自定义的style中没有指定的属性会继承parent style中的值。parent可以是android预定义的resource,也可以是自己定义的style。

  现在看看使用parent之后的样子:

  Xml代码 收藏代码

  < ?xml version="1.0" encoding="utf-8"?>

  < resources>

  < style name="homeiv">

  < item name="android:layout_width">wrap_content< /item>

  < item name="android:layout_height">wrap_content< /item>

  < item name="android:layout_marginTop">15dip< /item>

  < item name="android:layout_centerHorizontal">true< /item>

  < item name="android:background">@drawable/home< /item>

  < /style>

  < style name="homebtn" parent="@style/homeiv">

  < item name="android:textSize">20sp< /item>

  < item name="android:textColor">#FFFFffff< /item>

  < item name="android:gravity">center< /item>

  < item name="android:background">@drawable/selector< /item>

  < /style>

   < /resources>

  < ?xml version="1.0" encoding="utf-8"?>

  < resources>

  < style name="homeiv">

  < item name="android:layout_width">wrap_content< /item>

  < item name="android:layout_height">wrap_content< /item>

      < item name="android:layout_marginTop">15dip< /item>

  < item name="android:layout_centerHorizontal">true< /item>

  < item name="android:background">@drawable/home< /item>

  < /style>

  < style name="homebtn" parent="@style/homeiv">

  < item name="android:textSize">20sp< /item>

  < item name="android:textColor">#FFFFffff< /item>

  < item name="android:gravity">center< /item>

  < item name="android:background">@drawable/selector< /item>

  < /style>

  < /resources>

  当然style最主要的作用不是用来精简代码,而是让开发者自定义更个性的效果,不过这个附加的作用也不错。

分享到:
评论

相关推荐

    android style and theme

    **Style**可以在布局XML文件中通过`style`属性引用,而**Theme**则可以在活动或应用程序的配置文件中设置。具体来说,要在布局文件中应用一个**Style**,只需要在视图元素上设置`style`属性,例如: ```xml style=...

    Android 用户个人信息状态布局显示

    可以使用相对布局、约束布局等,配合`dimen.xml`文件中的尺寸资源,实现不同屏幕下的良好显示。 9. **可扩展性与复用性(Extensibility and Reusability)**: 一个好的自定义布局应该具有良好的扩展性和复用性,...

    android加载BaseActivity的布局

    我们可以创建一个XML布局文件,例如`activity_base.xml`,放在`res/layout`目录下,来定义这个自定义标题栏。 ```xml &lt;!-- res/layout/activity_base.xml --&gt; &lt;LinearLayout xmlns:android=...

    Android编写简易文件管理模块

    &lt;TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="文件/目录名"/&gt; ``` 为了将数据绑定到`ListView`,我们需要创建一个适配器,比如`ArrayAdapter`或`...

    Android style简单学习应用示例.zip

    "Android style简单学习应用示例.zip"提供了一个基础的源码示例,帮助初学者理解如何在Android应用中使用和自定义风格。 首先,让我们了解什么是Android Style。在Android中,风格是XML文件中的一组属性,定义了UI...

    安卓Android源码——android模仿易信UI布局效果源码.rar

    在Android中,UI布局主要通过XML文件定义,这些文件通常位于项目的res/layout目录下。易信UI布局的实现会涉及到各种布局容器,如LinearLayout、RelativeLayout、ConstraintLayout等,它们用于组织和定位子视图。通过...

    Android按钮控件的使用

    首先,要添加一个按钮到Android布局文件中,通常使用XML来定义。在`res/layout`目录下的XML布局文件中,我们可以使用`&lt;Button&gt;`标签来创建一个按钮。例如: ```xml android:id="@+id/my_button" android:layout_...

    自动生成Android适配不同机型、语言资源文件

    此外,适配工作还包括布局(layout)的调整,如使用相对布局(RelativeLayout)、约束布局(ConstraintLayout)等,以确保界面在不同屏幕尺寸上都能良好展示。还有主题(Theme)和样式(Style)的适配,可以通过创建...

    Android样式使用

    在Android中,为了减少重复代码并提高布局的复用性,可以使用`include`标签来引用已定义好的布局文件。这种方式非常适用于需要在多个地方复用相同布局的情况,比如顶部导航栏、底部导航栏等。 ### 示例:底部导航栏...

    Android_基础UI编程[1].pdf

    - Android 使用XML文件定义布局,如 `LinearLayout`,它是一种基本的布局容器,可以垂直或水平排列子视图。在XML中,`orientation` 属性决定了子视图的排列方式。 - `layout_height` 和 `layout_width` 属性定义了...

    解决Android Studio Design界面不显示layout控件的问题

    总结来说,解决Android Studio Design界面不显示layout控件的问题主要涉及到`styles.xml`文件的修改,特别是正确设置`Style`标签的`parent`属性。理解并熟练运用`styles.xml`文件和`Style`标签,不仅可以修复这类...

    android基本控件练习

    样式可以用来定义一组属性值的集合,以便在多个地方重复使用,简化XML文件。例如,在提供的示例中使用了以下两种样式: - **@style/TextViewTitleWidgetStyle**:用于设置`TextView`的标题样式。 - **@style/...

    Android UI设计技巧

    2. **在主布局中使用`include`标签**:接着,在需要使用共享布局的位置添加`&lt;include&gt;`标签,并通过`layout`属性指定要包含的布局文件。 3. **测试与调试**:最后,运行应用检查布局是否正确加载并显示。 **示例...

    Android 布局中的android:onClick的使用方法总结

    android:onClick 属性的使用方法非常简单,只需要在布局文件中添加 android:onClick 属性,并将其值设置为要调用的方法名称,该方法必须定义在 Activity 中,并且需要符合特定的参数和返回值类型。 例如,以下是...

    实现Splash的简化版本

    在`res/layout`目录下新建一个XML布局文件,例如`splash_activity.xml`: ```xml &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:...

    ex07_1_button.rar_android

    在Android布局XML文件中,我们可以使用`&lt;Button&gt;`标签来创建一个按钮。例如: ```xml android:id="@+id/myButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=...

    Android应用源码之10._LinearLayout学习-IT计算机-毕业设计.zip

    3. **子视图尺寸**:LinearLayout中的子视图可以使用`android:layout_width`和`android:layout_height`属性来设定尺寸,但当`layout_weight`非零时,这些属性通常设置为`0dp`(MATCH_PARENT的等价物,表示视图尺寸仅...

    android设置全局字体样式

    // 或者在XML布局文件中使用自定义字体 android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:typeface="@font/custom_font" /&gt; ``` 3. **通过...

    android 滚动数字选择器

    - 在布局XML文件中添加`TimePicker`控件: ```xml android:id="@+id/time_picker" android:layout_width="wrap_content" android:layout_height="wrap_content" android:timePickerMode="spinner" /&gt; ``` ...

    android样式(An-Beer工作室).pdf

    在Android开发中,样式(style)和主题(theme)是两个关键的概念,它们允许开发者为应用程序创建一致的视觉体验,同时简化代码。本篇文章将深入探讨这两个概念,并通过具体的例子来阐述如何定义和应用它们。 首先,...

Global site tag (gtag.js) - Google Analytics