zoukankan      html  css  js  c++  java
  • Android中动态改变控件的大小的一种方法

        在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课
    RUCSE小组博客列表
    test
    个人最终总结
    黄金点小游戏的设计与实现
    WordCount 程序的实现
    阅读下面程序,请回答如下问题:
    Visual Studio 2015的安装和简单的单元测试
    构建之法--软件工程学习随笔之二
  • 原文地址:https://www.cnblogs.com/platte/p/3477007.html
Copyright © 2011-2022 走看看