设为首页 加入收藏

TOP

Android开发:程序中动态设定组件的宽度、高度以及margin等属性
2014-11-24 11:36:41 来源: 作者: 【 】 浏览:0
Tags:Android 开发 程序 动态 设定 组件 宽度 高度 以及 margin 属性

注意:组件和控件是有区别的。组件对应的英文是component,控件对应的英文是control;控件是带有界面的,组件则未必有界面;控件属于组件,可以说它是带有界面的组件。比如Button有界面,因此可以说它是控件,也可以说它是组件。LinearLayout没有界面,因此它不能算是控件,但它却是组件。本文中由于涉及了带有和不带有界面的组件,因此,用组件泛指这两者。


有些组件,比如Button,可以在程序中用setWidth和setHeight来设定其大小,这是非常方便的。但有些组件却没有这两个设定大小的方法,比如ImageButton、Spinner以及LinearLayout等等,那么如何在程序中根据需要,动态地设定他们的大小呢?下面就用实际的例子来说明这个问题。




2. 将图片文件magnifier.png拖入到项目的res/drawable-mdpi文件夹下。mangifier.png的内容如下:




3. 在strings.xml中,增加如下粗体字代码。这些代码,将会被Spinner使用:


"spin_prompt">请选择城市


"cities">


北京


上海


南京


乌鲁木齐


哈尔滨


符拉迪沃斯托克




4. 修改main.xml,使之如下:


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



android:orientation="vertical"


android:layout_width="fill_parent"


android:layout_height="fill_parent"


>




android:layout_width="wrap_content"


android:layout_height="wrap_content"


android:text="@string/hello"


/>




android:layout_width="wrap_content"


android:layout_height="wrap_content"


android:src="@drawable/magnifier"


/>




android:layout_width="wrap_content"


android:layout_height="wrap_content"


android:entries="@array/cities"


android:prompt="@string/spin_prompt"


/>




android:orientation="horizontal"


android:layout_width="fill_parent"


android:layout_height="wrap_content"


>



android:orientation="horizontal"


android:layout_width="wrap_content"


android:layout_height="wrap_content"


android:gravity="left"


>



android:layout_width="wrap_content"


android:layout_height="wrap_content"


android:text="Hello"


/>





android:orientation="horizontal"


android:layout_width="wrap_content"


android:layout_height="wrap_content"


android:gravity="right"


>



android:layout_width="wrap_content"


android:layout_height="wrap_content"


android:text="Android"


/>






不难看出,mainx.ml有一个Button,一个ImageButton,一个Spinner和两个EditText。


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android开发:用getDrawingCache.. 下一篇Android开发:水平方向和垂直方向..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·求navicat for mysql (2025-12-26 13:21:33)
·有哪位大哥推荐一下m (2025-12-26 13:21:30)
·MySQL下载与安装教程 (2025-12-26 13:21:26)
·Linux_百度百科 (2025-12-26 12:51:52)
·Shell 流程控制 | 菜 (2025-12-26 12:51:49)