dynamic adding view [android]

发信息的页面那里是一个Layout布局,然后你每添加一幅图片,就在代码里生成一个ImageView实例并加入到这个Layout布局;每添加一个文字,就在代码里生成一个TextView实例并加入这个Layout布局;每添加一个按钮,就在代码里生成一个Button实例并加入这个Layout布局。这样就可以在这个Layout布局里面添加任意的控件,显示出来的效果就是可以在发信息的页面里面放置任意的图片、文字、按钮等。

创建一个新的layout newL, 将imageview加到newL, 再将newL加入到xml中定义的layout:

ViewGroup.LayoutParams lp_fullWidth =new ViewGroup.LayoutParams(lWidth, lHeight);
ViewGroup.LayoutParams lp_wrap =new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, lHeight);

LinearLayout layout = (LinearLayout) findViewById(R.id.viewNowPlaying);
LinearLayout newL = new LinearLayout(context); // create layout
ImageView arrowRT = new ImageView(context); // create image
arrowRT.setImageResource(R.drawable.arrowrt); // set source file
newL.addView(arrowRT, lp_wrap); // add image to newL
layout.addView(newL,lp_fullWidth); // add newL to layout

//相对布局

RelativeLayout relativeLayout = new RelativeLayout(this);
setContentView(relativeLayout);
AbsoluteLayout abslayout=new AbsoluteLayout (this);
RelativeLayout.LayoutParams lp1 = newRelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
lp1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp1.addRule(RelativeLayout.CENTER_HORIZONTAL,RelativeLayout.TRUE);
relativeLayout.addView(abslayout ,lp1);

//线性布局
LinearLayout ll = new LinearLayout(this);
EditText et = new EditText();
ll.addView(et);
//动态添加布局的方法1.

LinearLayout ll = (LinearLayout)this.getLayoutInflater().inflate(R.layout.main1,null);

setContentView(ll);

LinearLayout ll2 = (LinearLayout)this.getLayoutInflater().inflate(R.layout.main2,ll);

//这样 main2 作为 main1的子布局 加到了 main1的 根节点下

//动态添加布局的方法2 addView.

LinearLayout ll = (LinearLayout)this.getLayoutInflater().inflate(R.layout.main1,null);

setContentView(ll);

LinearLayout ll2 = (LinearLayout)this.getLayoutInflater().inflate(R.layout.main2,null);

ll.addView(ll2);

android:contentDescription="ImageButton Demo"//contentDescription是对图片的一个简要说明

在以前的顶级 布局上面 套一个 ScrollView:
scrollview not full screen:The issue is that adjustViewBounds will not increase the size of the ImageView beyond the natural dimensions of the drawable. It will only shrink the view to maintain aspect ratio; if you provide a 500x500 image instead of a 50x50 image, this should work.

activitygroup中的activity是通过容易addview  removeallview来实现显示和消失

ScrollView也是一个Layout布局,可以让它内部的数据显示不下的时候出现滚动条

  • 如果整个页面只有一个ListView的话,那么由于ListView本身带有滚动效果,所以当加载的数据超过页面显示的范围时,可以通过上下滑动来查看所有的item。
  • 如果布局A定义在ListView的前面,那么当布局A所占的比例较大,或者ListView加载的数据较多时,都会导致ListView显示不完全。同样,由于ListView自身可以滚动,因此仍然可以通过上下滚动来查看ListView的所有item。

android中ScrollView中TextView无法铺满全屏解决方案: 在ScrollView的xml中加入android:fillViewport=”true”属性就OK

给ScrollView设置属性:android:fillViewport=”true”
测试的时候发现,如果ListView加载的数据不多的话,确实可以解决,但是当ListView加载的数据较多的时候,仍旧无法显示完全,并且这个时候ListView自身也无法滚动了。

android屏幕监控上下左右滑动

view 或者 activity 实现 OnGestureListener 接口

  • 在 onFling方法中实现左右滑动:
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    		float y1 = e1.getY(), y2 = e2.getY();
    		if (y1 -y2 > 120) {  
    			if (mDirection != SOUTH) {
        			    mNextDirection = NORTH;
        		        }
            	Log.d(this.getClass().getName(), "To UP" + "(" + y1 + "," + y2 + ")");
    		return (true);
                    } else if (y1 - y2 < -120) {  
            	       if (mDirection != NORTH) {
        			   mNextDirection = SOUTH;
        		       }
            	Log.d(this.getClass().getName(), "To Down" + "(" + y1 + "," + y2 + ")");
    		return (true);
                    }  
    		return false;
    	}
  • 在 onScroll 方法中实现上下滑动:
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,	float velocityY) {
    	Log.d("Fling", "Fling Happened!");  
    	float x1 = e1.getX(), x2 = e2.getX();		
            if (x1 -x2 > 120) {  
            if (mDirection != EAST) {
    		mNextDirection = WEST;
    	}
            Log.d(this.getClass().getName(), "To LEFT" + "(" + x1+ "," + x2 + ")");
    	return (true);
            } else if (x1 - x2 < -120) {  
            	if (mDirection != WEST) {
    			mNextDirection = EAST;
    		}
            	Log.d(this.getClass().getName(), "To Right" + "(" + x1 + "," + x2 + ")");
    		return (true);
            }  
            		return false;
    	}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

生活在西班牙

自己动手丰衣足食

BlueAsteroid

Just another WordPress.com site

Jing's Blog

Just another WordPress.com site

Start from here......

我的心情魔方

天才遠私廚

希望能做一個分享各種資訊的好地方

语义噪声

西瓜大丸子汤的博客

笑对人生,傲立寰宇

Just another WordPress.com site

Where On Earth Is Waldo?

A Project By Melanie Coles

the Serious Computer Vision Blog

A blog about computer vision and serious stuff

Cauthy's Blog

paper review...

Cornell Computer Vision Seminar Blog

Blog for CS 7670 - Special Topics in Computer Vision

datarazzi

Life through nerd-colored glasses

Luciana Haill

Brainwaves Augmenting Consciousness

槑烎

1,2,∞

Dr Paul Tennent

and the university of nottingham

turn off the lights, please

A bunch of random, thinned and stateless thoughts around the Web

%d bloggers like this: