Android Seventh Party

  1. setColorFilter:

    The filter effect of the picture can be achieved through the setColorFilter method


     

  2. setTag () 、 getTag ():

    The setTag() method means to add extra data to the View, and then use getTag() to fetch this data. The label can be set arbitrarily.

    Let's take a look at the explanation of this in the 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.

Give a simple example:

Add a listener to multiple Buttons, and set a different setTag for each Button. The listener uses getTag to distinguish which Button was pressed.

code show as below:

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;

  }}}}

The explanation of this example is still clear. Have a simple understanding of Tag.


3. This is a diagram of the relationship between view and its subclasses:

image

 


4. I mentioned the addRule() method yesterday , and today I add a little content:

    Because there are many types of positioning methods in the relative layout, the API provides a unified method:

addRule(int verb, int anchor)

addRule(int verb)

Verb is the meaning of a verb, which is used to express above, below, toRightOf, toLeftOf, alignParentLeft... and so on.

The int values ​​of these verbs have constant definitions under RelativeLayout. E.g:

RelativeLayout.ABOVE

RelativeLayout.BELOW

RelativeLayout.ALIGN_LEFT

RelativeLayout.LEFT_OF

RelativeLayout.RIGHT_OF

The value of anchor can be RelativeLayout.TRUE, 0 means false, or other View Id, fill in the corresponding value according to the specific verb:

Give you an example:

First look at the renderings:

image

Next look at the code implementation: (hope to be patient, you will understand its usage after reading it carefully)

@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. Another problem is involved in the above code:

    lp1.addRule(RelativeLayout.ALIGN_LEFT, btn5.getId());

    lp3.addRule(RelativeLayout.ALIGN_BASELINE, btn5.getId());

What is this?

We have only mentioned 5 verbs above, and these two can be understood as baselines.

Take ALIGN_BASELINE as an example to explain through examples: add two adjacent TextViews, and give the second TextView a larger padding (such as 20dp). If you add ALIGN_BASELINE to the second TextView, see what is the difference.

(No baseline added)

 

image

(Join the baseline)

image

As can be seen from the above, the second TextView has moved up to ensure that hello and world are on the same horizontal line.

END


Due to CSDN typesetting restrictions, many tags are inconvenient to display. For details, please pay attention to GZH: Itchy Little Home

Originality is not easy, please pay attention and support~

Guess you like

Origin blog.csdn.net/allein_STR/article/details/113988822