在Android中有时候我们需要动态改变控件的大小。有几种办法可以实现

 一是在onMeasure中修改尺寸,二是在onLayout中修改位置和尺寸。这个是可以进行位置修改的,onMeasure不行。
还有一种是用LayoutParams来进行修改。前两种方法都需要你自定义控件,重载相关函数。二最后一种不需要重载。
今天,我要说的就是最后一种方法。先上代码:

        private void zoomInViewSize()
    {
        View img1 = findViewById(R.id.ImageView02);
        ViewGroup.LayoutParams  lp = img1.getLayoutParams();
        lp.width *=2;
        lp.height *=2; 
        img1.setLayoutParams(lp);

}


这是说把View的大小该为原来的大小的两倍大小。刚刚开始的时候,我测试不成功,其原因在于我的View的大小在布局文件中是
wrap_content的,这样,乘以2就没有效果了。有两种办法,一是在布局中将View的大小设置为一个固定尺寸。第二种是在这里
我们的lp.width和lp.height设置一个固定尺寸。不要再原来的基础上进行乘法和除法。但是你可以进行减法或加法的操作。
像这个样子:
        lp.width +=100;

lp.height +=100;


下面附上代码:
public class MainActivity extends Activity {
    protected static final String TAG = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    public void onClickZoomIn(View V)
    {
        zoomInViewSize();
        
    }
    
    public void onClickZoomOut(View V)
    {
        zoomOutViewSize();
        
    }
    
    private void zoomInViewSize()
    {
        View img1 = findViewById(R.id.ImageView01);
        ViewGroup.LayoutParams  lp = img1.getLayoutParams();
        lp.width *=2;
        lp.height *=2; 
        img1.setLayoutParams(lp);
    }
    
    //xiao
    private void zoomOutViewSize()
    {
        View img1 = findViewById(R.id.ImageView01);
        ViewGroup.LayoutParams  lp = img1.getLayoutParams();
        lp.width /=2;
        lp.height /=2; 
        
        img1.setLayoutParams(lp);
    }
}


<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    <ImageView
        android:id="@+id/ImageView01"
        android:layout_width="80dp"
        android:layout_height="100dp"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@+id/imageView1"
        android:src="@drawable/ic_launcher" />
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/ImageView01"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="47dp"
        android:layout_marginLeft="14dp"
        android:onClick="onClickZoomIn"
        android:text="ZoomIn" />
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_toRightOf="@+id/imageView1"
        android:onClick="onClickZoomOut"
        android:text="ZoomOut" />

</RelativeLayout>




Android中动态改变控件的大小的一种方法的更多相关文章

  1. WPF 中动态改变控件模板

    在某些项目中,可能需要动态的改变控件的模板,例如软件中可以选择不同的主题,在不同的主题下软件界面.控件的样式都会有所不同,这时即可通过改变控件模板的方式实现期望的功能. 基本方法是当用户点击切换主题按 ...

  2. Android之动态改变控件大小

    利用getLayoutParams()方法和setLayoutParams()方法.三步曲:1.首先利用getLayoutParams()方法,获取控件的LayoutParams.eg:LayoutP ...

  3. WPF中动态改变控件显示位置

    转自 http://blog.csdn.net/lassewang/article/details/6928897 测试环境: Windows XP/Windows 7 开发环境: Microsoft ...

  4. ios 运行时特征,动态改变控件字体大小

    需求:ex: 在不同尺寸的iPhone上面显示的字体大小不一样 https://github.com/rentzsch/jrswizzle #import <UIKit/UIKit.h> ...

  5. android 动态改变控件位置和大小 .

    动态改变控件位置的方法: setPadding()的方法更改布局位置. 如我要把Imageview下移200px:             ImageView.setPadding( ImageVie ...

  6. Android 在布局容器中动态添加控件

    这里,通过一个小demo,就可以掌握在布局容器中动态添加控件,以动态添加Button控件为例,添加其他控件同样道理. 1.addView 添加控件到布局容器 2.removeView 在布局容器中删掉 ...

  7. MFC中改变控件的大小和位置

    用CWnd类的函数MoveWindow()或SetWindowPos()可以改变控件的大小和位置. void MoveWindow(int x,int y,int nWidth,int nHeight ...

  8. MFC中改变控件的大小和位置(zz)

    用CWnd类的函数MoveWindow()或SetWindowPos()能够改变控件的大小和位置. void MoveWindow(int x,int y,int nWidth,int nHeight ...

  9. VC中动态添加控件

    VC中动态添加控件 动态控件是指在需要时由Create()创建的控件,这与预先在对话框中放置的控件是不同的. 一.创建动态控件: 为了对照,我们先来看一下静态控件的创建. 放置静态控件时必须先建立一个 ...

随机推荐

  1. HTML 中的特殊字符

    空格符         <小于号    < >大于号    > &和好      & ¥人民币  ¥ ©  版权   © ®  注册商标 ® ℃ 摄氏度  ° ...

  2. UVA 11987 Almost Union-Find (单点修改的并查集)

    此题最难处理的操作就是将一个单点改变集合,而普通的并查集是不支持这种操作的. 当结点p是叶子结点的时候,直接pa[p] = root(q)是可以的, p没有子结点,这个操作对其它结点不会造成任何影响, ...

  3. 网站被K怎么办?

    网站被K怎么办? 网站被K有几种状况,一种是网站的主页被删去或许网站悉数内容被删去就剩主页,还有一种是内容也在第“一”页,主页在后面,这种状况对初学者来讲是不会去留意的,他们会觉得这是正常的,其实这个 ...

  4. 为DataGridView控件实现复选功能

    实现效果: 知识运用: DataGridViewCheckBoxColumn类 实现代码: private class Fruit { public int Price { get; set; } p ...

  5. ps基础实例

    一:合并多个图片 1.先新件一个图片)CTRL+N),大小定成你想要的大小 2.把你要放入的照片用PS打开 3.把放入的照片用移动工具(V)拉到新件的图片里面 4.用CTRL+T调整大小(按住SHIF ...

  6. Bootstrap历练实例:导航内的下拉菜单

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  7. 学习笔记(三): Generalization/Overfitting/Validation

      目录 Generalization: Peril of Overfitting Low loss, but still a bad model? How Do We Know If Our Mod ...

  8. NOIP模拟赛 无线通讯网

    [题目描述] 国防部计划用无线网络连接若干个边防哨所.2种不同的通讯技术用来搭建无线网络:每个边防哨所都要配备无线电收发器:有一些哨所还可以增配卫星电话. 任意两个配备了一条卫星电话线路的哨所(两边都 ...

  9. 【贪心】bzoj1577: [Usaco2009 Feb]庙会捷运Fair Shuttle

    一类经典的线段贪心 Description 公交车一共经过N(1<=N<=20000)个站点,从站点1一直驶到站点N.K(1<=K<=50000)群奶牛希望搭乘这辆公交车.第i ...

  10. 【计数】hdu5921Binary Indexed Tree

    二进制拆位计算贡献 题目描述 树状数组是一种常用的数据结构,下面是树状数组用于给区间 [1,x] 内的数加 t 的代码: void add(int x,int t){ for (int i=x;i;i ...