Android控件组合应用二

本文介绍了一个具体的Android应用界面设计案例,包括操作条的布局及按钮的状态实现方式,通过XML定义不同状态下的图片资源,使按钮具备常规和按下两种状态。

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

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

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

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

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

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

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

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/title_new_selected" /> <item android:drawable="@drawable/title_new_normal" /> </selector>

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/title_reload_selected" /> <item android:drawable="@drawable/title_reload_normal" /> </selector>

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

<RelativeLayout android:layout_width="fill_parent" android:layout_height="44dp" android:background="@drawable/titlebar_lightgray_bg" android:orientation="horizontal"> <ImageButton android:id="@+id/BtnWrite" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_alignParentLeft="true" android:background="@android:color/transparent" android:src="@drawable/write_button"> </ImageButton> <TextView android:id="@+id/TextViewUsername" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textColor="@color/black" android:gravity="center" android:textSize="18sp"> </TextView> <ImageButton android:id="@+id/BtnRefresh" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_alignParentRight="true" android:background="@android:color/transparent" android:src="@drawable/refresh_button"> </ImageButton> </RelativeLayout>

最后,指定RelativeLayoutbackground为背景图片即可。

本次用到的图片有:

——欢迎转载,请注明出处 http://blog.youkuaiyun.com/caowenbin ——

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值