Android学习笔记の五
来源:互联网 发布:淘宝哪里取消申请退款 编辑:程序博客网 时间:2024/05/22 00:27
Android学习笔记の五
自定义控件
我们来自定义一个标题栏布局作为例子。
添加布局文件title.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/title_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:text="back" android:textColor="#000000" /> <TextView android:id="@+id/title_text" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="Title" android:textColor="#000000" android:textSize="24sp" /> <Button android:id="@+id/title_edit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:text="Edit" android:textColor="#000000" /></LinearLayout>
问:android:layout_gravity和android_gravity有什么区别?
可能你一下就被问蒙了 ( ̄y▽ ̄)~*
我来告诉你,android:layout_gravity是控件在布局文件中的位置,而android:gravity是控件中的文字在控件中的位置。
然后在acivity_main中这么写,我们丑陋的标题栏就做完了(不要在意细节(>﹏<))
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <include layout="@layout/title"/></LinearLayout>
需要什么布局只要include一下就好啦
我们的布局也绝不能是空架子!我们要让控件相应事件。
新建一个TitleLayout继承自LinearLayout
public class TitleLayout extends LinearLayout{ public TitleLayout(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub /*infate的两个参数,第一个是要加载的布局文件的id, *第二个是给加载好的布局再加一个父布局,这里我们想要指定为TitleLayout,所以传入this*/ LayoutInflater.from(context).inflate(R.layout.title, this); Button titleBack = (Button)findViewById(R.id.title_back); Button titleEdit = (Button)findViewById(R.id.title_edit); titleBack.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub /*销毁当前活动*/ ((Activity)getContext()).finish(); } }); titleEdit.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(getContext(), "Edit Clicked", Toast.LENGTH_SHORT).show(); } }); }}
从此再也不用重复写代码啦~
单位、尺寸
android设备屏幕的大小有N种,如何才能让你的应用在所有设备上都显示一直呢?
用dp表示大小!
dp是无关像素的意思,也称dip。 1dp是设备每英寸含的像素数
也就说dp是在按比例显示,而像素px是在按大小显示。比例是相对的,因此我们就解决了在不同分辨率的屏幕上显示的问题。
表示文字大小用sp,它和dp相类似。
sp和dp的区别
Android系统允许用户自定义文字尺寸大小(小、正常、大、超大等等),当文字尺寸是“正常”时1sp=1dp,而当文字尺寸是“大”或“超大”时,1sp>1dp英寸。
Nine-Patch图片
这是啥?有啥用?
。。。好,我们通过一个例子来说明。
先从网上随便照一张图(像素低一点)
然后这样修改activity_main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/message_left" ></LinearLayout></LinearLayout>
run一下,就变成了这幅样子
明显被拉伸了。好难看= =
这时候Nine-Patch就登场了
在Android sdk目录下的tools文件夹有一个draw9patch.bat,打开它
用它打开刚才的图片,如下图
(呃。。自动无视卖萌的输入法
我们可以在四个边框上绘制黑点,上边框和左边框表示图片要拉伸的位置,下边和右边表示内容会被放置的区域
看下图
然后把绘制好的图片保存起来替换掉原来的图片,重新run
是不是好了很多呢~
- Android学习笔记の五
- Android学习笔记五
- Android 学习笔记(五)
- Android学习笔记(五)
- android学习笔记(五)
- android系统学习笔记五
- Android学习笔记(五)
- android学习笔记(五)__Activity
- Android学习笔记(五)
- Android开发学习笔记(五)Android五大布局
- Android学习笔记(五):AndroidDebug
- android分层学习笔记(五)
- android学习笔记(五) Tab使用
- android多媒体部分学习笔记五----audio
- android入门:zxing学习笔记(五)
- android Fragment系列学习笔记之五
- Android学习笔记(五):布局控件
- Android学习笔记五 为应用程序打包
- 程序猿的日常-5
- HDU 1016 - Prime Ring Problem [简单DFS]
- Letter Combinations of a Phone Number
- 《AngularJS权威教程》
- F - Flowers
- Android学习笔记の五
- 15 Wallpaper 之OnTouch OnIntercept dispatch事件机制
- Raising Modulo Numbers (poj 1995 快速幂水题)
- c和c++的一些训练题(9)(学生成绩管理系统)
- HDU 4549 M斐波那契数列(矩阵快速幂)
- poj 3185 The Water Bowls (高斯消元)
- 怎样在Linux启动后执行指定的命令
- WindowsHOOK编程
- 黑马程序员--java基础--语言基础部分