-
setColorFilter:
通过setColorFilter方法可以实现图片的滤镜效果
-
setTag()、getTag():
setTag()方法表示给View添加一个额外的数据,然后用getTag()将这个数据取出来。标签可以任意设置。
我们来看一下SDK中对于这个的解释:
Tags :
Unlike IDs,Tags are not used to identify views,Tags are essentially an extra piece of iformation that can be assosciated with a view.They are most often used as a convenience to store data related to views in the views themselves rather than by putting them in a separate structure.
举个简单的例子:
在多个Button添加一个监听器,每个Button都设置不同的setTag。这个监听器就通过getTag来分辨是哪个Button 被按下。
代码如下:
public class Main extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button)
findViewById(R.id.Button01);
Button button2 = (Button)
findViewById(R.id.Button02);
Button button3 = (Button)
findViewById(R.id.Button03);
Button button4 = (Button)
findViewById(R.id.Button04);
MyListener listener = new MyListener();
button1.setTag(1);
button1.setOnClickListener(listener);
button2.setTag(2);
button2.setOnClickListener(listener);
button3.setTag(3);
button3.setOnClickListener(listener);
button4.setTag(4);
button4.setOnClickListener(listener);
}
public class MyListener implements View.OnClickListener
{
@Override
public void onClick(View v)
{
int tag = (Integer) v.getTag();
switch (tag) {
case 1:
System.out.println("button1 click");
break;
case 2:
System.out.println("button2 click");
break;
case 3:
System.out.println("button3 click");
break;
case 4:
System.out.println("button4 click");
break;
}}}}
这个例子讲解的还是听清楚的。对Tag有了简单的认识。
3.这是view和它的子类的一个关系图:
4. 昨天说过了addRule()方法,今天加上一点内容:
因为在相对布局中, 定位的方法有很多种类,所以API提供了一个统一的方法:
addRule(int verb, int anchor)
addRule(int verb)
verb是动词的意思,就是用来表达above, below, toRightOf, toLeftOf, alignParentLeft…..等等。
这些动词的int 值在RelativeLayout下有常量定义。例如:
RelativeLayout.ABOVE
RelativeLayout.BELOW
RelativeLayout.ALIGN_LEFT
RelativeLayout.LEFT_OF
RelativeLayout.RIGHT_OF
anchor的值,可以是RelativeLayout.TRUE,0表示false, 或者其它View 的Id, 根据具体的verb, 填入相应的值:
给大家一个例子:
先看一下效果图:
接下来看代码实现:(希望有点耐心,认真看过之后就会明白它的用法)
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setContentView(this.addRelativeLayout());
}
private RelativeLayout addRelativeLayout()
{
/*下面是定义了5个按钮:上下左右中*/
RelativeLayout container = new RelativeLayout(this);
Button btn1 = new Button(this);
btn1.setId(11);
btn1.setText("上");
Button btn2 = new Button(this);
btn2.setId(12);
btn2.setText("下");
Button btn3 = new Button(this);
btn3.setId(13);
btn3.setText("左");
Button btn4 = new Button(this);
btn4.setId(14);
btn4.setText("右");
Button btn5 = new Button(this);
btn5.setId(15);
btn5.setText("中");
/*new 5 个布局参数*/
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(100,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(lp1);
RelativeLayout.LayoutParams lp3 = new RelativeLayout.LayoutParams(lp1);
RelativeLayout.LayoutParams lp4 = new RelativeLayout.LayoutParams(lp1);
RelativeLayout.LayoutParams lp5 = new RelativeLayout.LayoutParams(lp1);
/*给每个参数添加规则,动态设置其相关位置*/
lp5.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
lp1.addRule(RelativeLayout.ABOVE, btn5.getId());
lp1.addRule(RelativeLayout.ALIGN_LEFT, btn5.getId());
lp2.addRule(RelativeLayout.BELOW, btn5.getId());
lp2.addRule(RelativeLayout.ALIGN_LEFT, btn5.getId());
lp3.addRule(RelativeLayout.LEFT_OF, btn5.getId());
lp3.addRule(RelativeLayout.ALIGN_BASELINE, btn5.getId());
lp4.addRule(RelativeLayout.RIGHT_OF, btn5.getId());
lp4.addRule(RelativeLayout.ALIGN_TOP, btn5.getId());
/*将5个view添加到容器中*/
container.addView(btn5, lp5);
container.addView(btn1, lp1);
container.addView(btn2, lp2);
container.addView(btn3, lp3);
container.addView(btn4, lp4);
return container;
}
5. 上面代码中又涉及到了一个其他的问题:
lp1.addRule(RelativeLayout.ALIGN_LEFT, btn5.getId());
lp3.addRule(RelativeLayout.ALIGN_BASELINE, btn5.getId());
这个是什么呢?
我们上面只说到了5种verb,而上面这两个可以理解为基准线。
以ALIGN_BASELINE为例,通过实例进行讲解:加入两个相邻的TextView,给第二个TextView一个大一点的padding(比如20dp)。如果在第二个TextView中添加ALIGN_BASELINE,看看有什么差别。
(未加基准线)
(加入基准线)
从上面可以看出来,第二个TextView向上移了,以保证hello和world处在同一水平线上。
END