android开发(13) 尝试在流布局中移动控件

来源:互联网 发布:quicktime 4 linux 编辑:程序博客网 时间:2024/05/17 08:21
我们常用的linearlayout,等都属于流布局,在流布局中如何移动控件呢? 我决定做个尝试。虽然可以使用绝对布局,但我不倾向使用这个布局。那么看看我的方式吧。

 

记得margin这个属性吗,我们就用来它来控制控件的位置,改动它的值将会产生移动的效果。


 

 

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

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

            

 如上面的代码所示,margin的属性存在于 布局参数LayoutParams中。

1。我们先获得该控件的 布局参数 然后转型为ViewGroup.MarginLayoutParams 

2. 更改margin的数值,通过更改 该控件的上下左右偏移量(相对于父容器控件的原点),来更改控件的呈现位置。

3. 调用requestLayout 请求重新布局。

 通过上面的方式,我们可以产生控件移动的效果。

 

--------------

 同时,我们了解下 ScroolBy这个方法,该方法可以产生控件的滚动效果。而看起来移动了该控件的子内容。

 

              textView1.scrollBy(15, 15); 
  

 

 该方法需要两个参数,x轴偏移量和y轴偏移量。执行代码后,我们看到产生了 类似 滚动条移动后,控件 上移 的效果。看起来像是重绘了视图内容,而变化了绘制的坐标原点。

类似的还有个scroolTo方法,该方法需要制定目的偏移量。

 

贴完整的示例代码如下:

 <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="控件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"
            android:layout_height="wrap_content"
            android:text="改动marinLeft 控件1" />

        <Button
            android:id="@+id/btnScroll"
            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 控件1" />

        <Button
            android:id="@+id/btnScrollParent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="scrollBy  控件1 的父控件" />
    </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 + "的状态:\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, "控件1的父 ");
        s += GetTextStateOfView(textView1, "\n控件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;
    }
}

 

代码下载

 

 

 

 

0 0