Android 动态生成布局 (多层嵌套)

时间:2022-09-08 11:37:41

Android 除了可以加载xml文件,显示布局外,也可以代码生成布局,并通过setContentView(View view)方法显示布局。单独的一层布局,如一个主布局加一个控件(如Button\imageView等)动态生成代码比较简单,下面只给出示例代码:

package com.example.android_dongtaishengcheng;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.RelativeLayout.LayoutParams;

public class MainActivity extends Activity
{
RelativeLayout relativeLayout = null;
private Button button;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
relativeLayout = new RelativeLayout(this);
LayoutParams params = new LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
relativeLayout.setLayoutParams(params);
relativeLayout.setBackgroundResource(R.color.back);
setContentView(relativeLayout);

button = new Button(this);
LayoutParams params2 = new LayoutParams(300, 300);
button.setLayoutParams(params2);
params2.addRule(RelativeLayout.CENTER_IN_PARENT);
button.setText("hello");
relativeLayout.addView(button);
button.setOnClickListener(new View.OnClickListener()
{

@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this,SecondActivity.class));
}
});
}
}
下面进入重点:多层嵌套布局的动态生成。

情景描述:父布局是一个线性布局,其子布局按竖直方向排列,子布局的子布局也是一个线性布局,按水平方向排列。
其实很简单,关键点在于,如何控制主布局的子布局换行显示,即实现Orientation = "vertical"。可以在子布局外在加一层布局,即下面的drawParent()方法,该方法用来生成父布局的直接子布局,drawView()方法用来生成直接子布局的子布局(该布局也是多层嵌套)。可以实现如下图效果:

Android  动态生成布局  (多层嵌套)

贴出代码如下:

/**
*
*/
package com.example.android_dongtaishengcheng;
import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.LinearLayout.LayoutParams;

/**
* @author zhiyuan
*
* 2014-5-29 上午10:44:44
*
*/
public class SecondActivity extends Activity
{
LinearLayout layout = null;
LinearLayout line2 = null;
LinearLayout line3 = null;
LinearLayout line4 = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
layout = new LinearLayout(this);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
layout.setLayoutParams(params);
layout.setOrientation(LinearLayout.VERTICAL);
setContentView(layout);

// layout.addView(drawView(this));
line2 = (LinearLayout) drawParent(this);
line3 = (LinearLayout) drawParent(this);
line4 = (LinearLayout) drawParent(this);
for (int i = 0; i < 2; i++)
{
line2.addView(drawView(SecondActivity.this, i));
}
for (int i = 0; i < 4; i++)
{
line3.addView(drawView(SecondActivity.this, i));
}
for(int i = 0; i < 5; i++){
line4.addView(drawView(SecondActivity.this, i));
}
layout.addView(line2);
layout.addView(line3);
layout.addView(line4);
}
//生成子布局的子布局
public View drawView(Context context, int count)
{

LinearLayout layout = new LinearLayout(context);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT, 1);
params.gravity = Gravity.CENTER;
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setLayoutParams(params);
if (count == 1)
{
layout.setBackgroundResource(R.color.back);
}
RelativeLayout relativeLayout = new RelativeLayout(context);
android.widget.RelativeLayout.LayoutParams params3 = new android.widget.RelativeLayout.LayoutParams(
android.widget.RelativeLayout.LayoutParams.MATCH_PARENT,
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayout.setLayoutParams(params3);
/*
* ImageView imageView = new ImageView(context); LayoutParams params2 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
*/
Button imageView = new Button(context);
android.widget.RelativeLayout.LayoutParams params2 = new android.widget.RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
//imageView.setGravity(RelativeLayout.CENTER_IN_PARENT);
imageView.setText("测试");
params2.addRule(RelativeLayout.CENTER_IN_PARENT);

// imageView.setBackgroundResource(R.drawable.ic_launcher);
imageView.setLayoutParams(params2);

/*
* TextView textView = new TextView(context); LayoutParams params3 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); textView.setText("测试专用");
* textView.setLayoutParams(params3);
*/

relativeLayout.addView(imageView);
// layout.addView(textView);
layout.addView(relativeLayout);
return layout;
}
//生成主布局的子布局
public View drawParent(Context context)
{
LinearLayout layout = new LinearLayout(context);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
// params.gravity = Gravity.CENTER_HORIZONTAL;
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setLayoutParams(params);
return layout;
}

}

附Demo下载地址: http://download.csdn.net/detail/laoziyueguo3/7423939