动态设置view或布局的宽高

本文介绍了如何在Android应用中动态地改变图片或布局的大小,通过Java代码设置View或布局的宽高。强调了不能直接新建LayoutParams并设置宽高,而应该先获取当前的LayoutParams,然后修改高度和宽度。同时提醒注意根据父视图类型选择正确的LayoutParams子类,例如RelativeLayout.LayoutParams、LinearLayout.LayoutParams或ViewGroup.LayoutParams。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


代码中动态设置view或布局的宽高

 

有时我们需要在应用中动态改变图片或某一块布局的大小。这就不能用XML文件写成固定值,而需要在java代码中动态设置。效果如下:

     

网上有一些教程使用relativeView.setLayoutParams(new RelativeLayout.LayoutParams(100,200));的方法,可是发现这样设置很容易抛错;

因此有人指出不能直接新建一个LayoutParams的同时设置宽高值,需要先实例化一个对象,再进行具体参数的设置,然后再设置,如下:

RelativeLayout.LayoutParams Params =  (RelativeLayout.LayoutParams)mView.getLayoutParams();
        Params.height = 100;
        mView.setLayoutParams(linearParams);

然而这时候你一定要注意强制类型转换时的LayoutParams类型,因为android中存在3种LayoutParams,即RelativeLayout.LayoutParams、LinearLayout.LayoutParams、ViewGroup.LayoutParams,那么我们改用哪一个呢?

--要看你要操作的view在布局文件中的父控件是什么类型的,若父控件是RelativeLayout则需要强制转换为RelativeLayout.LayoutParams,其它类型依次类推。


Aactivity代码:

  1. package com.example.setwidthheight;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.content.res.Resources;  
  6. import android.os.Bundle;  
  7. import android.util.TypedValue;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.Button;  
  11. import android.widget.EditText;  
  12. import android.widget.ImageView;  
  13. import android.widget.RelativeLayout;  
  14. import android.widget.Toast;  
  15.   
  16. public class MainActivity extends Activity {  
  17.       
  18.     private EditText editWidth;  
  19.     private EditText editHeight;  
  20.     private ImageView imageView;  
  21.     private Button button;  
  22.       
  23.   
  24.     @Override  
  25.     protected void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.activity_main);  
  28.         editWidth = (EditText) findViewById(R.id.edit_width);  
  29.         editHeight = (EditText) findViewById(R.id.edit_height);  
  30.         imageView = (ImageView) findViewById(R.id.img);  
  31.         button    = (Button) findViewById(R.id.btn);  
  32.           
  33.         button.setOnClickListener(changeClickListener);  
  34.           
  35.     }  
  36.       
  37.     private OnClickListener changeClickListener = new OnClickListener() {  
  38.         @Override  
  39.         public void onClick(View v) {  
  40.             if (editHeight.getText() != null && editWidth.getText() != null  
  41.                     && !editHeight.getText().toString().equals("")  
  42.                     && !editWidth.getText().toString().equals("")) {  
  43.   
  44.                 int width = Integer.parseInt(editWidth.getText().toString());  
  45.                 int height = Integer.parseInt(editHeight.getText().toString());  
  46.   
  47.                 RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) imageView.getLayoutParams();  
  48.                 params.width = dip2px(MainActivity.this, width);  
  49.                 params.height = dip2px(MainActivity.this, height);  
  50.                 // params.setMargins(dip2px(MainActivity.this, 1), 0, 0, 0); // 可以实现设置位置信息,如居左距离,其它类推  
  51.                 // params.leftMargin = dip2px(MainActivity.this, 1);  
  52.                 imageView.setLayoutParams(params);  
  53.   
  54.             } else {  
  55.                 Toast.makeText(MainActivity.this"请输入宽高!", Toast.LENGTH_LONG).show();  
  56.             }  
  57.   
  58.         }  
  59.     };  
  60.       
  61.     /** 
  62.      * dp转为px 
  63.      * @param context  上下文 
  64.      * @param dipValue dp值 
  65.      * @return 
  66.      */  
  67.     private int dip2px(Context context,float dipValue)   
  68.     {  
  69.         Resources r = context.getResources();  
  70.         return (int) TypedValue.applyDimension(  
  71.                 TypedValue.COMPLEX_UNIT_DIP, dipValue, r.getDisplayMetrics());  
  72.     }  
  73.       
  74. }  

XML布局代码:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context="${relativePackage}.${activityClass}" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/tv"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_centerHorizontal="true"  
  12.         android:layout_marginBottom="10dp"  
  13.         android:text="输入宽高后,点击按钮改变大小" />  
  14.       
  15.     <ImageView   
  16.         android:id="@+id/img"  
  17.         android:layout_width="200dp"  
  18.         android:layout_height="300dp"  
  19.         android:layout_below="@+id/tv"  
  20.         android:contentDescription="@null"  
  21.         android:layout_centerHorizontal="true"  
  22.         android:src="@drawable/image"  
  23.         />  
  24.       
  25.     <LinearLayout   
  26.         android:layout_width="fill_parent"  
  27.         android:layout_height="wrap_content"  
  28.         android:layout_above="@+id/btn"  
  29.         android:layout_marginBottom="10dp"  
  30.         android:padding="5dp"  
  31.         android:orientation="horizontal"  
  32.         >  
  33.   
  34.         <TextView  
  35.             android:layout_width="fill_parent"  
  36.             android:layout_height="wrap_content"  
  37.             android:layout_weight="1.2"  
  38.             android:text="设置 (dp)  " />  
  39.           
  40.         <EditText   
  41.             android:id="@+id/edit_width"  
  42.             android:layout_width="fill_parent"  
  43.             android:layout_height="wrap_content"  
  44.             android:layout_weight="1"  
  45.             android:hint="宽"  
  46.             />  
  47.           
  48.         <EditText   
  49.             android:id="@+id/edit_height"  
  50.             android:layout_width="fill_parent"  
  51.             android:layout_height="wrap_content"  
  52.             android:layout_weight="1"  
  53.             android:hint="高"  
  54.             />  
  55.           
  56.     </LinearLayout>  
  57.       
  58.     <Button   
  59.         android:id="@+id/btn"  
  60.         android:layout_width="wrap_content"  
  61.         android:layout_height="wrap_content"  
  62.         android:layout_marginBottom="10dp"  
  63.         android:layout_centerHorizontal="true"  
  64.         android:layout_alignParentBottom="true"  
  65.         android:text="change"  
  66.         />  
  67.   
  68. </RelativeLayout>  


源码下载地址:http://download.youkuaiyun.com/detail/duguju/9302619

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值