有时我们需要在应用中动态改变图片或某一块布局的大小。这就不能用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代码:
- package com.example.setwidthheight;
-
- import android.app.Activity;
- import android.content.Context;
- import android.content.res.Resources;
- import android.os.Bundle;
- import android.util.TypedValue;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ImageView;
- import android.widget.RelativeLayout;
- import android.widget.Toast;
-
- public class MainActivity extends Activity {
-
- private EditText editWidth;
- private EditText editHeight;
- private ImageView imageView;
- private Button button;
-
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- editWidth = (EditText) findViewById(R.id.edit_width);
- editHeight = (EditText) findViewById(R.id.edit_height);
- imageView = (ImageView) findViewById(R.id.img);
- button = (Button) findViewById(R.id.btn);
-
- button.setOnClickListener(changeClickListener);
-
- }
-
- private OnClickListener changeClickListener = new OnClickListener() {
- @Override
- public void onClick(View v) {
- if (editHeight.getText() != null && editWidth.getText() != null
- && !editHeight.getText().toString().equals("")
- && !editWidth.getText().toString().equals("")) {
-
- int width = Integer.parseInt(editWidth.getText().toString());
- int height = Integer.parseInt(editHeight.getText().toString());
-
- RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
- params.width = dip2px(MainActivity.this, width);
- params.height = dip2px(MainActivity.this, height);
- // params.setMargins(dip2px(MainActivity.this, 1), 0, 0, 0); // 可以实现设置位置信息,如居左距离,其它类推
- // params.leftMargin = dip2px(MainActivity.this, 1);
- imageView.setLayoutParams(params);
-
- } else {
- Toast.makeText(MainActivity.this, "请输入宽高!", Toast.LENGTH_LONG).show();
- }
-
- }
- };
-
- /**
- * dp转为px
- * @param context 上下文
- * @param dipValue dp值
- * @return
- */
- private int dip2px(Context context,float dipValue)
- {
- Resources r = context.getResources();
- return (int) TypedValue.applyDimension(
- TypedValue.COMPLEX_UNIT_DIP, dipValue, r.getDisplayMetrics());
- }
-
- }
XML布局代码:
- <RelativeLayout xmlns:android="http://schemas./apk/res/android"
- xmlns:tools="http://schemas./tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="${relativePackage}.${activityClass}" >
-
- <TextView
- android:id="@+id/tv"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:layout_marginBottom="10dp"
- android:text="输入宽高后,点击按钮改变大小" />
-
- <ImageView
- android:id="@+id/img"
- android:layout_width="200dp"
- android:layout_height="300dp"
- android:layout_below="@+id/tv"
- android:contentDescription="@null"
- android:layout_centerHorizontal="true"
- android:src="@drawable/image"
- />
-
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_above="@+id/btn"
- android:layout_marginBottom="10dp"
- android:padding="5dp"
- android:orientation="horizontal"
- >
-
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1.2"
- android:text="设置 (dp) " />
-
- <EditText
- android:id="@+id/edit_width"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:hint="宽"
- />
-
- <EditText
- android:id="@+id/edit_height"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:hint="高"
- />
-
- </LinearLayout>
-
- <Button
- android:id="@+id/btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="10dp"
- android:layout_centerHorizontal="true"
- android:layout_alignParentBottom="true"
- android:text="change"
- />
-
- </RelativeLayout>
源码下载地址:http://download.csdn.net/detail/duguju/9302619
|