程式碼中動態設定view或佈局的寬高
阿新 • • 發佈:2019-02-15
有時我們需要在應用中動態改變圖片或某一塊佈局的大小。這就不能用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佈局程式碼:
原始碼下載地址:http://download.csdn.net/detail/duguju/9302619<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" 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>