Android seventh lie down

  1. setColorFilter:

    The image filter effect can be realized through setColorFilter method


  2. setTag(),getTag():

    The setTag() method means to add an additional data to the View, and then use getTag() to get the 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.

Take a simple example:

Add a listener to multiple buttons, and each Button sets a different setTag. The listener uses getTag to distinguish which Button is pressed.

The code is as follows:

public class Main extends Activity 



  public void onCreate(Bundle savedInstanceState) 




      Button button1 = (Button) 


      Button button2 = (Button) 


      Button button3 = (Button) 


      Button button4 = (Button) 


      MyListener listener = new MyListener();










  public class MyListener implements View.OnClickListener 



      public void onClick(View v) 


          int tag = (Integer) v.getTag();

          switch (tag) {

          case 1:

          System.out.println("button1 click");


          case 2:

          System.out.println("button2 click");


          case 3:

          System.out.println("button3 click");


          case 4:

          System.out.println("button4 click");



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:


4. The addRule() method was mentioned yesterday, and a little content is added today:

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

addRule(int verb, int anchor)

addRule(int verb)

Verb is a verb meaning, which is used to express above, below, to right of, to left of, align parent left wait.

The verb RelativeLayout has values under these constants. For example:






The value of anchor can be relativelayout True, 0 means false, or the Id of other views. Fill in the corresponding value according to the specific verb:

Let me give you an example:

Let's take a look at the renderings first:

Next, let's look at the code implementation: (I hope to be a little patient. After careful reading, I will understand its usage)


    protected void onCreate(Bundle savedInstanceState) 





    private RelativeLayout addRelativeLayout() 


        /*Here are five buttons defined: up, down, left, right, middle*/

        RelativeLayout container = new RelativeLayout(this);

        Button btn1 = new Button(this);



        Button btn2 = new Button(this);



        Button btn3 = new Button(this);



        Button btn4 = new Button(this);



        Button btn5 = new Button(this);




        /*new 5 Layout parameters*/

        RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(100,


        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);

        /*Add rules to each parameter and dynamically set its relevant position*/

        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());


         /*Add 5 view s to the container*/

        container.addView(btn5, lp5);

        container.addView(btn1, lp1);

        container.addView(btn2, lp2);

        container.addView(btn3, lp3);

        container.addView(btn4, lp4);


        return container;



5. The above code involves another problem:

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

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

What is this?

We only mentioned five kinds of verb above, and the above two can be understood as baselines.

Align_ Take baseline as an example to explain through examples: add two adjacent textviews and give the second TextView a larger padding (such as 20dp). If align is added in the second TextView_ Baseline, see what the difference is.

(without reference line)


(add baseline)

As can be seen from the above, the second TextView moves upward to ensure that hello and world are on the same level.


Due to CSDN typesetting restrictions, many marks are inconvenient to express. For details, please pay attention to GZH: little home with pain and itch

Original is not easy, please pay more attention and support~

Tags: Design Pattern Android

Posted by nrg_alpha on Sat, 16 Apr 2022 12:28:20 +0930