加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

动态添加控件及将某XML动态加入到Activity显示

发布时间:2020-12-16 00:32:07 所属栏目:百科 来源:网络整理
导读:本文第二篇:《 动态添加综合布局---动态添加控件及将某XML动态加入到Activity显示(续) 》 一、动态添加控件、设置参数 这个难度比较大,放在前面讲,用的也比较多,普通情况下,我们会提前把布局XML写好,然后对XML中的元素进行设置,但这种方法在有些情

本文第二篇:《动态添加综合布局---动态添加控件及将某XML动态加入到Activity显示(续)

一、动态添加控件、设置参数

这个难度比较大,放在前面讲,用的也比较多,普通情况下,我们会提前把布局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);

5、添加对新增的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,8);
tv_like.setText("赞(8)");
tv_like.setTextSize(16);	
layout_sub_Lin.addView(tv_like,LP_LIKE_MW);




上源码(两个例子代码混合在一起):http://download.csdn.net/detail/harvic880925/6829633(不要分,仅供分享)

请大家尊重原创者版权,转载请标明出处:http://www.52php.cn/article/p-ftqkgfos-bae.html 谢谢!!!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读