自从学习安卓开发时一直都是用的是xml可视化布局,觉得十分方便直观,偶尔也会用到动态布局但却不是很熟悉究竟如何用。最近做项目需要从网络获取图片并且自己处理放大,就有点懵逼,想到动态布局实现却不知该怎么用(欲哭无泪,说白了还是技术太渣),所以写下这篇日志,警醒自己学无止境。希望正在和我一同进步的童鞋,一起茁长成长
废话不多说了,内容如下:介绍多种实现动态布局的方法,以及如何用代码来调整View位置
这里只介绍三种布局情况(注意不是方式)
1、无xml : 一个父类布局包含一个子父类布局,子父类布局中包含ImageView
2、无xml : 只有一个父类布局包含一个ImageView
3、有xlm布局: 通过布局ID 来进行动态布局添加
总结了下其实步骤如下:
无xml布局:
1、setContentView()之前new一个需要的布局layout,再将layout放入setContentView()
2、new 出需要的控件设置好参数(id、text···)
3、new LayoutParams 设置好控件的大小、位置属性(这里感觉和xml设置控件属性是一样的)
4、最后将params和控件放入之前new的layout即可
有xml布局:
1、setContentView()和以前一样放入layout.xml
2、通过findViewById()找到要进行添加的布局控件
之后的步骤和无xml布局的2、3、4一样
代码如下:
1、无xml : 一个父类布局包含一个子父类布局,子父类布局中包含ImageView
RelativeLayout relativeLayout = new RelativeLayout(this);
setContentView(relativeLayout);
RelativeLayout rl = new RelativeLayout(this);
rl.setId(11);
ImageView imageView = new ImageView(this);
imageView.setId(1);
imageView.setImageResource(R.mipmap.ic_launcher);
RelativeLayout.LayoutParams lpRl = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
rl.setGravity(RelativeLayout.CENTER_IN_PARENT); //设置imageView 在 rl中的位置为居中
rl.addView(imageView, lpRl);
RelativeLayout.LayoutParams lpParent = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
relativeLayout.addView(rl,lpParent);
2、无xml : 只有一个父类布局包含一个ImageView
RelativeLayout relativeLayout = new RelativeLayout(this);
setContentView(relativeLayout);
ImageView imageView = new ImageView(this);
imageView.setId(2);
imageView.setImageResource(R.mipmap.ic_launcher);
//params 可以理解为 imageView的位置、大小参数集合
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
relativeLayout.addView(imageView,params);
3、有xlm布局: 通过布局ID 来进行动态布局添加
public class ThirdActivity extends AppCompatActivity {
private LinearLayout mLinearLayout;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
mLinearLayout = (LinearLayout) findViewById(R.id.linear_layout);
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.mipmap.ic_launcher);
imageView.setId(31);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(150, 80, 10, 0);
mLinearLayout.addView(imageView, params);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
是不是很简单啊,了解到原理后对以后一些需要动态变化的布局操作起来就十分的方便了
补充一个 : 动态改变xml控件(width、height、padding...)
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="点击我改变大小"/>
</RelativeLayout>
对要修改的控件进行setLayoutParams(params),注意:params 最好不好用 new 的方式,可能会出错
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mButton.getLayoutParams();
public class MainActivity extends AppCompatActivity{
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mButton.getLayoutParams();
params.width = dip2px(MainActivity.this, dip2px(MainActivity.this, 300));
params.height = dip2px(MainActivity.this, dip2px(MainActivity.this, 100));
mButton.setLayoutParams(params);
// mButton.setPadding(300,0,0,0);
}
});
}
/**
* dp转为px
* @param context 上下文
* @param dipValue dp值
* @return
*/
public int dip2px(Context context, float dipValue)
{
Resources r = context.getResources();
return (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, dipValue, r.getDisplayMetrics());
}
}
赠人玫瑰,手有余香。您的支持是我创作最大的动力!