Android UI控件组合应用之二:按钮布局

移动开发 Android
本文以Android版本的新浪微博手机客户端为例,介绍了Android代码如何实现微博主体列表框。作者与大家分享了如何使用ImageButton控件和RelativeLayout对按钮进行设置和布局。

在上一篇文章中,我们已经完成了数据模型的代码,并且为了测试方便,在类中直接为很多成员变量提供了默认值。接下来,进入到界面部分。

纵观整个界面,可以分成上下两块,一块是顶端的操作条,另一块是占主体的列表框。

先从顶端的操作条开始,在这里,很容易分解成三个部分,左侧的写微博按钮,中间的用户名显示,右侧的刷新按钮。两个按钮的风格是一样的,都是有常规和按下两种状态,这种按钮是非常常用的,我的做法是:

1. 在drawable文件夹下建立两个xml文件,分别对应了两个按钮;

2. 每个xml文件中使用selector标签定义常规状态和选中状态的两个图片资源;

3. 在Activity的布局中使用ImageButton,指定按钮的background为透明,并指定src为刚才定义的两个xml。

下面是这两个xml文件的内容:

 

  1. view plaincopy to clipboardprint?  
  2. <?xml version="1.0" encoding="utf-8"?>    
  3. <selector xmlns:android="http://schemas.android.com/apk/res/android">    
  4.     <item android:state_pressed="true" android:drawable="@drawable/title_new_selected" />    
  5.     <item android:drawable="@drawable/title_new_normal" />    
  6. </selector> 

 

 

 

  1. view plaincopy to clipboardprint?  
  2. <?xml version="1.0" encoding="utf-8"?>    
  3. <selector xmlns:android="http://schemas.android.com/apk/res/android">    
  4.     <item android:state_pressed="true" android:drawable="@drawable/title_reload_selected" />    
  5.     <item android:drawable="@drawable/title_reload_normal" />    
  6. </selector> 

 

 

在main.xml文件中,进行这一部分的布局,对于这三个界面元素而言,有明确的位置关系,因此采用RelativeLayout合适,内容如下:

 

  1. view plaincopy to clipboardprint?  
  2. <RelativeLayout      
  3.     android:layout_width="fill_parent" android:layout_height="44dp"    
  4.     android:background="@drawable/titlebar_lightgray_bg" android:orientation="horizontal">    
  5.     <ImageButton android:id="@+id/BtnWrite"    
  6.         android:layout_width="wrap_content" android:layout_height="fill_parent"    
  7.         android:layout_alignParentLeft="true" android:background="@android:color/transparent"    
  8.         android:src="@drawable/write_button">    
  9.     </ImageButton>    
  10.     <TextView android:id="@+id/TextViewUsername"    
  11.         android:layout_width="fill_parent" android:layout_height="fill_parent"    
  12.         android:textColor="@color/black" android:gravity="center" android:textSize="18sp">    
  13.     </TextView>    
  14.     <ImageButton android:id="@+id/BtnRefresh"    
  15.         android:layout_width="wrap_content" android:layout_height="fill_parent"    
  16.         android:layout_alignParentRight="true" android:background="@android:color/transparent"    
  17.         android:src="@drawable/refresh_button">    
  18.     </ImageButton>    
  19. </RelativeLayout> 

 

 

 

最后,指定RelativeLayout的background为背景图片即可。

本次用到的图片有:

               

责任编辑:佚名 来源: CSDN博客
相关推荐

2011-05-17 17:12:39

2011-03-11 10:35:31

SQL锁定SQL Server

2011-04-11 15:57:22

DFSBFSC++

2011-08-15 10:15:00

iPhone开发警告框

2014-01-03 13:56:00

手游用户体验设计启动和停止

2011-08-10 15:36:26

iPhone静态库控件

2022-02-17 20:07:45

Flex鸿蒙Flex组件

2017-02-13 17:17:48

Android标题栏控件

2010-05-13 15:18:19

Subversion插

2011-05-30 08:58:59

Android focusable 属性

2011-09-07 14:34:55

Android Wid控件

2013-01-08 16:05:23

Android开发布局ViewStub

2011-05-30 17:02:56

Android Activity 传参与跳转

2013-08-09 14:23:09

Android应用Android退出按钮Android开发设计

2011-04-08 13:58:10

Android界面设计

2009-01-13 14:49:14

Winform视频教程控件

2018-06-08 15:28:31

Android开发程序

2021-12-20 21:01:52

鸿蒙HarmonyOS应用

2011-06-24 16:27:41

QML UI

2018-08-15 12:43:52

iOSAndroid设计差异
点赞
收藏

51CTO技术栈公众号