Android开发(13) 移动View

概述

我们常用的linearlayout,等都属于流布局,在流布局中如何移动控件呢? 我决定做个尝试。虽然可以使用绝对布局,但我不倾向使用这个布局。那么看看我的方式吧。

margin方式

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 请求重新布局。

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

ScrollBy方式

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

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

代码下载

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,636评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,890评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,680评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,766评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,665评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,045评论 1 276
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,515评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,182评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,334评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,274评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,319评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,002评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,599评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,675评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,917评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,309评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,885评论 2 341

推荐阅读更多精彩内容