码迷,mamicode.com
首页 > 移动开发 > 详细

Android动态添加textview组件和imageview组件

时间:2017-04-01 01:26:33      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:ids   .com   image   字符串   生成   build   create   ima   group   

import android.app.Activity;

import android.os.Bundle;

import android.view.Gravity;

import android.view.View;

import android.view.ViewGroup;

import android.widget.AdapterView;

import android.widget.BaseAdapter;

import android.widget.Gallery;

import android.widget.ImageView;

import android.widget.LinearLayout;

import android.widget.ListView;

import android.widget.TextView;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.AdapterView.OnItemSelectedListener;

public class Sample_5_4 extends Activity {

//所有资源图片(andybilledgartorvaldsturingid的数组

int[] drawableIds=

{R.drawable.andy,R.drawable.bill,R.drawable.edgar,R.drawable.torvalds,R.drawable.turing};

//所有资源字符串(andybilledgartorvaldsturingid的数组

int[] msgIds={R.string.andy,R.string.bill,R.string.edgar,R.string.torvalds,R.string.turing};

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

ListView lv=(ListView)this.findViewById(R.id.ListView01);//初始化ListView

BaseAdapter ba=new BaseAdapter(){//ListView准备内容适配器

public int getCount() {return 5;}//总共5个选项

public Object getItem(int arg0) { return null; }

public long getItemId(int arg0) { return 0; }

public View getView(int arg0, View arg1, ViewGroup arg2) {

//动态生成每个下拉项对应的View,每个下拉项ViewLinearLayout

//中包含一个ImageView及一个TextView构成

LinearLayout ll=new LinearLayout(Sample_5_4.this);//初始化LinearLayout

ll.setOrientation(LinearLayout.HORIZONTAL); //设置朝向

ll.setPadding(5,5,5,5);//设置四周留白

ImageView ii=new ImageView(Sample_5_4.this);//初始化ImageView

ii.setImageDrawable(getResources().getDrawable(drawableIds[arg0]));//设置图片

ii.setScaleType(ImageView.ScaleType.FIT_XY);

ii.setLayoutParams(new Gallery.LayoutParams(100,98));

ll.addView(ii);//添加到LinearLayout

TextView tv=new TextView(Sample_5_4.this);//初始化TextView

tv.setText(getResources().getText(msgIds[arg0]));//设置内容

tv.setTextSize(24);//设置字体大小

tv.setTextColor(Sample_5_4.this.getResources().getColor(R.color.white));//设置字体颜色

tv.setPadding(5,5,5,5);//设置四周留白

tv.setGravity(Gravity.LEFT);

ll.addView(tv);//添加到LinearLayout

return ll;

}

};

lv.setAdapter(ba);//ListView设置内容适配器

lv.setOnItemSelectedListener(//设置选项选中的监听器

new OnItemSelectedListener(){

public void onItemSelected(AdapterView<?> arg0, View arg1,

int arg2, long arg3) {//重写选项被选中事件的处理方法

TextView tv=(TextView)findViewById(R.id.TextView01);//获取主界面TextView

LinearLayout ll=(LinearLayout)arg1;//获取当前选中选项对应的LinearLayout

TextView tvn=(TextView)ll.getChildAt(1);//获取其中的TextView

StringBuilder sb=new StringBuilder();//StringBuilder动态生成信息

sb.append(getResources().getText(R.string.ys));

sb.append(":");

sb.append(tvn.getText());

String stemp=sb.toString();

tv.setText(stemp.split("\\n")[0]);//信息设置进主界面TextView

}

public void onNothingSelected(AdapterView<?> arg0){}

}

);

lv.setOnItemClickListener(//设置选项被单击的监听器

new OnItemClickListener(){

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,

long arg3) {//重写选项被单击事件的处理方法

TextView tv=(TextView)findViewById(R.id.TextView01);//获取主界面TextView

LinearLayout ll=(LinearLayout)arg1;//获取当前选中选项对应的LinearLayout

TextView tvn=(TextView)ll.getChildAt(1);//获取其中的TextView

StringBuilder sb=new StringBuilder();//StringBuilder动态生成信息

sb.append(getResources().getText(R.string.ys));

sb.append(":");

sb.append(tvn.getText());

String stemp=sb.toString();

tv.setText(stemp.split("\\n")[0]);//信息设置进主界面TextView

}

}

);

}

}

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<TextView

android:id="@+id/TextView01"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textSize="24dip"

android:textColor="@color/white"

android:text="@string/hello"/>

<ListView

android:id="@+id/ListView01"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:choiceMode="singleChoice"/>

</LinearLayout>

Android动态添加textview组件和imageview组件

标签:ids   .com   image   字符串   生成   build   create   ima   group   

原文地址:http://www.cnblogs.com/wx459173/p/6654014.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!