android开发步步为营之101:动态调整view的宽度和高度以及在页面的坐标位置

第一步、页面设计





第二步、Activity设计

package com.figo.study.activity; import android.app.ActionBar; import android.graphics.Color; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; import com.figo.study.R; public class LayoutParametersActivity extends AppCompatActivity { TextView mTxtView; RelativeLayout mRelativeLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_layout_parameters); mRelativeLayout=(RelativeLayout)findViewById(R.id.layout_main); mTxtView=(TextView)findViewById(R.id.txt_meme); adjustWidthHeight(mTxtView,300,200); addView(); adjustXY(mTxtView,100,500); } //调整控件宽度和高度 private void adjustWidthHeight(View v,int width,int height) { ViewGroup.LayoutParams lp=v.getLayoutParams(); lp.width=width; lp.height=height; v.setLayoutParams(lp); } //已有的view动态调整其left,top坐标 publicvoid adjustXY(View view,int x,int y) { ViewGroup.MarginLayoutParams margin=new ViewGroup.MarginLayoutParams(view.getLayoutParams()); margin.setMargins(x, y, x + margin.width, y + margin.height); RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(margin); view.setLayoutParams(layoutParams); } //动态添加的view调整其left,top坐标 private void addView() { ImageView imgText = new ImageView(LayoutParametersActivity.this); imgText.setImageResource(R.mipmap.ic_launcher); imgText.setBackgroundColor(Color.parseColor("#ffb6b6")); RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(100, 100); layoutParams.topMargin=200; layoutParams.leftMargin=100; layoutParams.rightMargin=8; layoutParams.bottomMargin=8; mRelativeLayout.addView(imgText, layoutParams); }}



【android开发步步为营之101:动态调整view的宽度和高度以及在页面的坐标位置】

    推荐阅读