我们常用的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;
    }
}

代码下载

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

  1. Android开发 ---如何操作资源目录中的资源文件2

    Android开发 ---如何操作资源目录中的资源文件2 一.颜色资源管理 效果图: 描述: 1.改变字体的背景颜色 2.改变字体颜色 3.改变按钮颜色 4.图像颜色切换 操作描述: 点击(1)中的颜 ...

  2. Android开发模式万佛朝中MVX(MVC、MVP、MVVM)

    今天公司的测试服务器开小差了,后台被吐槽的体无完肤,虽然我们都知道跟他没有什么关系,但是生活需要乐趣,总要有人背锅,哈哈!~~~暂时没有环境开发了,那就分享一下之前做的一篇关于Android开发模式的 ...

  3. 【Android 开发入门】我认识中的Android

    2014年12月从csdn专家福利获得的一本书<Android游戏开发技术实战详解>,尘封了一年多的时间,今天才翻开来看. 我认识中的Android,提到Android最先浮现在我脑海中的 ...

  4. webos开发 html,尝试在WebOS中创建HTML5表,但失败

    我尝试使用以下JavaScript在WebOS中创建HTML5表,但未创建表. 但是,除了第一个Mojo.log"尝试创建数据库"之外,我无法在日志中看到任何进一步的细节.尝试在W ...

  5. Android开发之关于MVVM架构中视图数据绑定框架dataBinding的基本用法

    dataBinding是Google官方开发的第三方视图数据绑定框架.优缺点如下: 优点:很好用 缺点:调试bug不易,部分AS版本中不太友好 首先说下如何使用: 在gradle中的android模块 ...

  6. 解放双手 - Android 开发应该尝试的 UI 自动化测试

    本文由玉刚说写作平台提供写作赞助 原作者:却把青梅嗅 版权声明:本文版权归微信公众号玉刚说所有,未经许可,不得以任何形式转载 困境 接下来我将说到这种情况并非个例--作为一个Android开发者,当我 ...

  7. 解放双手,Android开发应该尝试的UI自动化测试

    本文由 玉刚说写作平台 提供写作赞助 原作者:却把清梅嗅 原文地址:https://mp.weixin.qq.com/s/ODbqUHjQUTA79UyI5Fw5Mw 版权声明:本文版权归微信公众号 ...

  8. Android开发——如何解决三方库中的类名冲突问题

    文章目录 背景 一.尝试复现 二.初步想法 三.继续思考 3.1 源码引入 3.2 是否可以经过二次混淆改名 3.3 Android Transform 3.4 直接修改本地aar文件 3.4.1 找 ...

  9. Android开发学习:在Eclipse中导入Android项目方法

    在Eclipse中导入Android项目方法的具体步骤如下: 1.启动Eclipse,依次选择File---Import,如下图所示: 2.在弹出的Import窗口中选择Existing Projec ...

最新文章

  1. 【Android开发】范例1-实现带描边的圆角图片
  2. 用户界面改变图片锚点
  3. 【数据分析】数据缺失影响模型效果?是时候需要missingno工具包来帮你了!
  4. ubuntu下MySQL的安装
  5. CodeForces - 1420D Rescue Nibel!(组合数学+离散化)
  6. rpc 服务器不可用_什么是远程过程调用RPC
  7. mediarecorder 录制的文件无法拖动进度条_录制课程不用愁,小V手把手教学
  8. Flutter 静态挂载腾讯X5WebView(Tbs)浏览器内核
  9. 如何通过努力出书,如何写有畅销资质的书,本文汇集了多位计算机图书作者的经验
  10. 环境保护设施运营组织服务认证
  11. 数据库 实验六 存储过程
  12. 利用SSM(springmvc+spring+mybatis)实现多表联合查询
  13. 悟空问答死于知乎十周年
  14. linux mmc 读写,linux内核mmc读写分析
  15. java根据内容生成二维码并保存到本地
  16. 台式计算机屏幕扩展,电脑扩展显示器调整的方法
  17. 背景动态变化登陆界面
  18. 史上最浅显易懂的Git学习指南
  19. oracle Number字段类型 对于小数位数的理解
  20. 对timedelta64的理解

热门文章

  1. centOS6.5如何从启动界面直接进入命令行界面和如何从图形界面进入命令行界面
  2. node.js 笔记1 模块方面
  3. 【剑指offer】_18 数据流中的中位数
  4. 计算机网络(一)计算机网络体系
  5. Gdb 调试core文件详解
  6. hibernate Criteria(条件查询接口)
  7. 280. Wiggle Sort
  8. 在UITouch事件中画圆圈-iOS8 Swift基础教程
  9. 联系表单 1_copy
  10. (转)MVC模式参数传递的探究