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

是不是好了很多呢~

更多相关文章

  1. Android四种常用布局
  2. RelativeLayout各个属性
  3. android 焦点问题
  4. 整理分享ImageView属性大全
  5. android 按钮设计中state_selected属性
  6. Your content must have a ListView whose id attribute is 'and
  7. android中ImageView的ScaleType属性
  8. [Android] AsyncTask使用实例---加载网络图片
  9. Android(安卓)Layout XML属性

随机推荐

  1. Android获取有线和无线(wifi)的IP地址
  2. Android(安卓)Canvas
  3. android 将bitmap缓存到本地
  4. Android获取apk的版本及包名等信息
  5. Android(安卓)自定义Progress Bar
  6. android创建自定义对话框
  7. Android(安卓)获得可用摄像头id
  8. Android的报错信息at com.android.ddmlib
  9. 2011.10.10(2)——— android Clipping关于
  10. Android调用Webview中的js方法