Android 動態生成佈局 (多層巢狀)
阿新 • • 發佈:2019-02-17
前段時間學習了android,瞭解到android主要是使用xml佈局檔案進行佈局的,但是對於一些動態的介面,就需要使用程式碼動態生成佈局。
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;
- publicclass MainActivity extends Activity
- {
-
RelativeLayout relativeLayout = null
- private Button button;
- @Override
- protectedvoid 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
- publicvoid onClick(View v)
- {
- // TODO Auto-generated method stub
- startActivity(new Intent(MainActivity.this,SecondActivity.class));
- }
- });
- }
- }
情景描述:父佈局是一個線性佈局,其子佈局按豎直方向排列,子佈局的子佈局也是一個線性佈局,按水平方向排列。
其實很簡單,關鍵點在於,如何控制主佈局的子佈局換行顯示,即實現Orientation = "vertical"。可以在子佈局外在加一層佈局,即下面的drawParent()方法,該方法用來生成父佈局的直接子佈局,drawView()方法用來生成直接子佈局的子佈局(該佈局也是多層巢狀)。可以實現如下圖效果:
貼出程式碼如下:
- /**
- *
- */
- 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
- *
- */
- publicclass SecondActivity extends Activity
- {
- LinearLayout layout = null;
- LinearLayout line2 = null;
- LinearLayout line3 = null;
- LinearLayout line4 = null;
- @Override
- protectedvoid 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