本例实现了动态修改ImageView控件的宽高度,有两个按钮,一个按钮实现放大image,一个按钮实现缩小image

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center"
android:orientation="horizontal"> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="放大" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="缩小" />
</LinearLayout> <ImageView
android:id="@+id/imageView"
android:layout_width="150dp"
android:layout_height="150dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/img01" /> </android.support.constraint.ConstraintLayout>

MainActivity.java

package com.example.chenrui.app1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); final ImageView image = findViewById(R.id.imageView);
Button button1 = findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
ViewGroup.LayoutParams params = image.getLayoutParams();
params.width = params.width + 50;
params.height = params.height + 50;
image.setLayoutParams(params);
}
}); Button button2 = findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
ViewGroup.LayoutParams params = image.getLayoutParams();
params.width = params.width - 50;
params.height = params.height - 50;
image.setLayoutParams(params);
}
});
}
}

实现的效果:

android中动态修改ImageView控件的宽高度的更多相关文章

  1. android中一个评分的控件

    RatingBar android中一个评分的控件 如何使用 Android Studio下: dependencies { compile 'com.hedgehog.ratingbar:app:1 ...

  2. 谨记给UpdatePanel中动态添加的控件赋ID

    原文:谨记给UpdatePanel中动态添加的控件赋ID 昨天下定决 心对上次做的布局编辑器控件加以改进,其中最主要变化的就是要完全使用ASP.NET AJAX!但是很遗憾,虽然耳闻已久,但目前对AS ...

  3. duilib 修复padding属性导致其他控件自动计算宽高度错误的bug和导致自己宽高度错误的bug

    转载请说明原出处,谢谢~~:http://blog.csdn.net/zhuhongshu/article/details/42950733          BUG 一:padding导致其他控件宽 ...

  4. Android UI 统一修改Button控件的样式,以及其它系统控件的默认样式

    先介绍下修改原理:首先打开位于android.widget包下面的Button.java文件,这里有一句关键的代码如下: public Button(Context context, Attribut ...

  5. Android中的自定义视图控件

    简介 当现有控件不能满足需求时,就需要自定义控件. 自定义控件属性 自定义控件首先要继承自View,重写两个构造函数. 第一个是代码中使用的: public MyRect(Context contex ...

  6. VS中查看/修改Dialog控件TAB顺序的方法

    打开资源视图,打开Dialog的编辑界面 查看: 格式>Tab键顺序 修改: 格式>Tab键顺序 用鼠标左键按你想要的顺序点击各个控件的TAB标签,就设定了 那些你想要TAB键能选择到的控 ...

  7. Android中使用shape来定义控件

    本文章转接于:http://kofi1122.blog.51cto.com/2815761/521605 Android中常常使用shape来定义控件的一些显示属性,今天看了一些shape的使用,对s ...

  8. android中的五大布局(控件的容器,可以放button等控件)

    一.android中五大布局相当于是容器,这些容器里可以放控件也可以放另一个容器,子控件和布局都需要制定属性. 1.相对布局:RelativeLayout @1控件默认堆叠排列,需要制定控件的相对位置 ...

  9. android 中通过代码创建控件

    package bvb.de.openadbwireless.circle; import android.annotation.TargetApi; import android.app.Activ ...

随机推荐

  1. Android Service总结01 目录

    Android Service总结01 目录 1 Android Service总结01 目录 2 Android Service总结02 service介绍 介绍了“4种service 以及 它们的 ...

  2. C#编程(三十二)----------数组基础

    数组 如果需要使用同一类型的多个对象,就可以使用数组.数组是一种数据结构,他可以包含同一类型的多个元素. 数组的声明 在声明数组时,应先定义数组中元素的类型,其后是一对方括号核一遍变量名.例如:生命一 ...

  3. 平台升级至spring 4.3.0 运行稳定

    deprecated velocity 部份方法 整理中...

  4. centos7编译安装nginx及无缝升级https

    安装依赖: yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel 下载nginx: wget -c  ...

  5. Material Designer的低版本兼容实现(九)—— Float Button & Small Float Button

    5.0一个新特性就是出现了这么一个圆形的悬浮指示按钮,这个按钮可以用来体现一个全局的重要功能,比如添加账户什么的.这个按钮有两种大小,一种是正常的按钮大小,一种是小型的按钮.官方文档中介绍的是小心的按 ...

  6. Easyui 页面设置加载完成之后,满屏

    js文件: if(top.location!=self.location){ top.location.href=self.location; }

  7. Can't create pdf file with font calibri bold 错误解决方案

    错误情况: %%[ ProductName: Distiller ]%% Mangal not found, using Courier. %%[ Error: invalidfont; Offend ...

  8. 帕累托分布(Pareto distributions)、马太效应

    什么是帕累托分布 帕累托分布是以意大利经济学家维弗雷多·帕雷托命名的. 是从大量真实世界的现象中发现的幂次定律分布.这个分布在经济学以外,也被称为布拉德福分布. 帕累托因对意大利20%的人口拥有80% ...

  9. Useful JVM Flags – Part 8 (GC Logging)

    The last part of this series is about garbage collection logging and associated flags. The GC log is ...

  10. Mongodb的windows服务安装和卸载

    不用 InstallUtil.exe,直接用mongod.exe做就可以: 安装:mongod --dbpath "C:\mongodb\db" --logpath "C ...