An overview of the

We commonly use linearLayout, etc., which belong to the flow layout, how to move the control in the flow layout? I decided to give it a try. While absolute layouts can be used, I prefer not to use this one. So look at my way.

Margin way

Margin property, which specifies the margin. We use it to control the position of the control, changing its value will have the effect of moving.

ViewGroup.MarginLayoutParams paras = (ViewGroup.MarginLayoutParams) textView1
                    .getLayoutParams();

            paras.setMargins(paras.leftMargin + 15, paras.topMargin + 15,
                    paras.rightMargin, paras.bottomMargin);
            textView1.requestLayout();
Copy the code

As shown in the code above, the margin attribute exists in the layout parameter LayoutParams.

  1. Let’s get the layout of the control parameters And then transition to ViewGroup. MarginLayoutParams
  2. Change the value of margin to change the rendering position of the control by changing its upper, lower, left, and right offsets relative to the origin of the parent container control.
  3. Call requestLayout to request a relayout.

In this way, we can create the effect of the control moving.

ScrollBy way

Also, take a look at the ScrollBy method, which produces the scrolling effect of the control. It appears that the child content of the control has been moved.

          textView1.scrollBy(15, 15); 
Copy the code

This method takes two parameters, the X-axis offset and the Y-axis offset. After executing the code, we see that the control moves up as the scroll bar moves. It looks like you’ve redrawn the contents of the view and changed the origin of the coordinates drawn.

Similarly, there is a scroolTo method that needs to specify the destination offset.

The complete sample code is as follows:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/LinearLayout1" android:layout_width="fill_parent" android:layout_height="200dp" android:layout_alignParentTop="true" android:background="#426ab3" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="140dp" android:layout_height="60dp" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_marginLeft="25dp" Android :background="# FFFFFF "Android :gravity="center" Android :text=" control 1" tools:context=".MainActivity" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="wrap_content" <Button android:id="@+id/btnScroll" android:layout_height="wrap_content" Android :text=" marinLeft control 1" /> android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="16dp" Android :text="scrollBy 1" /> <Button android:id="@+id/btnScrollTo1" Android :layout_width="wrap_content" Android :layout_height="wrap_content" Android :text="crollTo control 1" /> <Button Android :id="@+id/btnScrollParent" Android :layout_width="wrap_content" Android :layout_height="wrap_content" Android :text="scrollBy 1 parent control "/> </LinearLayout> <TextView android:id="@+id/txtState" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/LinearLayout1" android:layout_marginLeft="5dp" android:layout_marginTop="25dp" android:text="info:" /> </RelativeLayout> package com.example.zyf.demo; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; public class MainActivity extends Activity { TextView textView1; TextView txtState; Button btn1; Button btnScroll; Button btnScrollTo1; Button btnScrollParent; LinearLayout linearLayout1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView1 = (TextView) findViewById(R.id.textView1); linearLayout1 = (LinearLayout) findViewById(R.id.LinearLayout1); btn1 = (Button) findViewById(R.id.button1); btn1.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { // textView1.setPadding(textView1.getPaddingLeft()+15, // textView1.getPaddingTop(), textView1.getPaddingRight(), // textView1.getPaddingBottom()); ViewGroup.MarginLayoutParams paras = (ViewGroup.MarginLayoutParams) textView1 .getLayoutParams(); paras.setMargins(paras.leftMargin + 15, paras.topMargin + 15, paras.rightMargin, paras.bottomMargin); textView1.requestLayout(); //textView1.invalidate(); PrintfState(); }}); btnScroll = (Button) findViewById(R.id.btnScroll); btnScroll.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { textView1.scrollBy(15, 15); //textView1.requestLayout(); PrintfState(); }}); btnScrollTo1 = (Button) findViewById(R.id.btnScrollTo1); btnScrollTo1.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { textView1.scrollTo(15, 15); PrintfState(); }}); btnScrollParent = (Button) findViewById(R.id.btnScrollParent); btnScrollParent.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { linearLayout1.scrollBy(15, 15); PrintfState(); }}); txtState = (TextView) findViewById(R.id.txtState); PrintfState(); } private String GetTextStateOfView(View View, String title) {StringBuilder sb = new StringBuilder(title + "status :\n"); sb.append(String.format("ScrollX:%s ,ScrollY:%s", view.getScrollX(), view.getScrollY())); ViewGroup.MarginLayoutParams paras = (ViewGroup.MarginLayoutParams) view .getLayoutParams(); sb.append(String.format("margins: %s,%s,%s,%s", paras.leftMargin, paras.topMargin, paras.rightMargin, paras.bottomMargin)); return sb.toString(); } private void PrintfState() { String s=""; S += GetTextStateOfView(linearLayout1, "parent of control 1 "); S += GetTextStateOfView(textView1, "\n control 1"); Printf(s); } private void Printf(String str) { txtState.setText(str); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; }}Copy the code

The code download