一、動態添加控件、設置參數
這個難度比較大,放在前面講,用的也比較多,普通情況下,我們會提前把佈局XML寫好,然後對XML中的元素進行設置,但這種方法在有些情況下就顯得不適合,比較聊天應用,比如帖子的回覆情況。針對這些情況,我們要動態根據獲取到的數據增加控件或控件組的數量,廢話不多說,下面就開整吧,先看個效果圖:
原始XML 動態添加控件後
所做的工作:
1、在原有的界面的基礎上添加一個LinearLayout layout;參數設置爲:layout_width:wrap_content;layout_height:wrap_content;
對應代碼:
LinearLayout layout = new LinearLayout(this); // 線性佈局方式
layout.setOrientation(LinearLayout.HORIZONTAL); //
layout.setBackgroundColor(0xff00ffff);
LinearLayout.LayoutParams LP_MM = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
layout.setLayoutParams(LP_MM);
2、添加一個ImageView;參數設置成layout_width:50;layout_height:50;
ImageView imageView= new ImageView(this);
imageView.setBackgroundResource(R.drawable.maomao);
LinearLayout.LayoutParams PARA = new LinearLayout.LayoutParams(50,50);
imageView.setLayoutParams(PARA);
layout.addView(imageView);
3、添加一個TextView;參數設置成layout_width:wrap_content;layout_height:wrap_content;
對應代碼:
TextView tv = new TextView(this); // 普通聊天對話
tv.setText("我和貓貓是新添加的");
tv.setBackgroundColor(Color.GRAY);
LinearLayout.LayoutParams LP_WW = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(LP_WW);
layout.addView(tv);
4、獲取當前佈局,即當前main_activity的LinearLayout佈局(這裏有兩種方法)
方法一:(這種方法不需要:setContentView(R.layout.activity_main);)
// 獲取需要被添加控件的Linear佈局(方法一)
final LayoutInflater inflater = LayoutInflater.from(this);
LinearLayout lin = (LinearLayout) inflater.inflate(
R.layout.activity_main, null).findViewById(
R.id.mainLinearLayout);
方法二:
// 獲取需要被添加控件的Linear佈局(方法二)
setContentView(R.layout.activity_main);
final LinearLayout lin = (LinearLayout) findViewById(R.id.mainLinearLayout);
5、將動態增加的佈局添加到當前佈局中並顯示;
lin.addView(layout);
setContentView(lin);
6、添加對新增的ImageView的單擊消息響應
//向動態添加的imageView,添加點擊響應
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "點擊了圖片", Toast.LENGTH_SHORT).show();
}
});
全部代碼:
package com.example.try_add_combination_ctrl;
/**
* 動態增加控件組
* @author harvic
* @date 2014-1-9
*/
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// // 獲取需要被添加控件的Linear佈局(方法一)
// final LayoutInflater inflater = LayoutInflater.from(this);
// LinearLayout lin = (LinearLayout) inflater.inflate(
// R.layout.activity_main, null).findViewById(
// R.id.mainLinearLayout);
// 獲取需要被添加控件的Linear佈局(方法二)
setContentView(R.layout.activity_main);
final LinearLayout lin = (LinearLayout) findViewById(R.id.mainLinearLayout);
// 添加一個LinearLayout佈局,設置成layout_width:wrap_content;layout_height:wrap_content;
LinearLayout layout = new LinearLayout(this); // 線性佈局方式
layout.setOrientation(LinearLayout.HORIZONTAL); //
layout.setBackgroundColor(0xff00ffff);
LinearLayout.LayoutParams LP_MM = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
layout.setLayoutParams(LP_MM);
//添加一個ImageView,設置成layout_width:50;layout_height:50;
ImageView imageView = new ImageView(this);
imageView.setBackgroundResource(R.drawable.maomao);
LinearLayout.LayoutParams PARA = new LinearLayout.LayoutParams(50, 50);//
imageView.setLayoutParams(PARA);
layout.addView(imageView);
//添加一個TextView,設置成layout_width:wrap_content;layout_height:wrap_content;
TextView tv = new TextView(this); // 普通聊天對話
tv.setText("我和貓貓是新添加的");
tv.setBackgroundColor(Color.GRAY);
LinearLayout.LayoutParams LP_WW = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(LP_WW);
layout.addView(tv);
//將動態增加的佈局添加到當前佈局中;
lin.addView(layout);
setContentView(lin);
// 向動態添加的imageView,添加點擊響應
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "點擊了圖片", Toast.LENGTH_SHORT)
.show();
}
});
}
}
二、將某一XML動態加入到當前Activity顯示
這裏就跟上面的不一樣了,上面的是動態生成的控件或控件組,但這裏並不是動態生成的,只是將一個寫好的XML在運行時加入到當前Activity的XML中顯示;
先看XML佈局吧
1、原來的XML(activity_main.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00ff00"
android:text="我是原生的,下面的佈局是添加的"
android:textSize="16sp" />
</LinearLayout>
2、要增加進去的XML(combination_ctrl.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/combineCtrl"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView android:id="@+id/img"
android:layout_width="100dip"
android:layout_height="100dip"
android:layout_margin="10.0dip"
android:padding="2.0dip"
android:scaleType="fitXY"/>
<LinearLayout android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF00"
android:textSize="22px" />
<TextView android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF00FFFF"
android:textSize="13px" />
</LinearLayout>
</LinearLayout>
看看效果:
原形狀態 增加進去後
全部代碼
package com.example.try_add_layout_from_xml;
/**
* 將一個現有的XML代碼加入到當前的XML中,但由於ID是一定的,所以與在代碼中添加include效果一樣
* @author harvic
* @date 2014-1-9
*/
import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LayoutInflater inflater = LayoutInflater.from(this);
// 獲取需要被添加控件的佈局
final LinearLayout lin = (LinearLayout) findViewById(R.id.mainLinearLayout);
// 獲取需要添加的佈局
LinearLayout layout = (LinearLayout) inflater.inflate(
R.layout.combination_ctrl, null).findViewById(R.id.combineCtrl);
// 將佈局加入到當前佈局中
lin.addView(layout);
ImageView imageView = (ImageView) findViewById(R.id.img);
imageView.setBackgroundResource(R.drawable.maomao);
TextView TV_info = (TextView) findViewById(R.id.info);
TV_info.setText("第一個INOF");
TextView TV_name = (TextView) findViewById(R.id.name);
TV_name.setText("第一個NAME");
}
}
可見代碼非常短,而且關鍵代碼就只有五句,專門列出來
setContentView(R.layout.activity_main);
final LayoutInflater inflater = LayoutInflater.from(this);
// 獲取需要被添加控件的佈局
final LinearLayout lin = (LinearLayout) findViewById(R.id.mainLinearLayout);
// 獲取需要添加的佈局
LinearLayout layout = (LinearLayout) inflater.inflate(
R.layout.combination_ctrl, null).findViewById(R.id.combineCtrl);
// 將佈局加入到當前佈局中
lin.addView(layout);
三、相關代碼設置參數彙總
1、設置margin
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(10, 20, 30, 40);
imageView.setLayoutParams(lp);
2、設置layout_weight:
setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT,weight
));
例如:
TextView tv_like = new TextView(this);
LinearLayout.LayoutParams LP_LIKE_MW = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
tv_like.setGravity(Gravity.CENTER);
tv_like.setPadding(0, 8, 0, 8);
tv_like.setText("贊(8)");
tv_like.setTextSize(16);
layout_sub_Lin.addView(tv_like, LP_LIKE_MW);