API Demo Snake代码分析一 FrameLayout新的认识

以前写界面的布局主要用的是LinearLayout和RelativeLayout,其他的就用的很少了,今天在看之前参照api demo中Snake的例子写的程序,看到他唯一的那个界面就是用FrameLayout写的,之前也一直对FrameLayout不甚了解,今天认真总结一下。下面是API中的解释

FrameLayout is designed to block out an area on the screen to display a single item. You can add multiple children to a FrameLayout and control their position within the FrameLayout usinggravity. Children are drawn in a stack, with the most recently added child on top. The size of the frame layout is the size of its largest child (plus padding), visible or not (if the FrameLayout's parent permits). Views that are GONE are used for sizing only if setConsiderGoneChildrenWhenMeasuring() is set to true.

主要的关键点是:在FrameLayout里面要想设置子控件的布局要使用gravity;子控件都在一个栈里面进行绘画,最近添加的子空间在最上面;整个FrameLayout的大小是和最大的子空间一样大的。

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout 
  xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" > 
 
    <ImageView 
        android:id="@+id/image" 
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent" 
        android:scaleType="center" 
        android:src="@drawable/candle" 
        /> 
    <TextView 
        android:id="@+id/text1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center" 
        android:textColor="#00ff00" 
        android:text="@string/hello" 
        /> 
    <Button 
        android:id="@+id/start" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="bottom" 
        android:text="Start" 
        /> 
</FrameLayout>

在apidemo中的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="@+id/frameLayout1"
	android:layout_width="fill_parent" android:layout_height="fill_parent"
	xmlns:android="http://schemas.android.com/apk/res/android">

	<com.gao.apidemo.snake.SnakeView
		android:id="@+id/snake" android:layout_width="fill_parent"
		android:layout_height="fill_parent" tileSize="24" />
	<RelativeLayout android:id="@+id/relativeLayout1"
		android:layout_width="fill_parent" android:layout_height="fill_parent">
		<TextView android:layout_height="wrap_content" android:id="@+id/textView"
			android:layout_centerInParent="true" android:layout_width="wrap_content"
			android:text="TextView" android:textColor="#ff8888ff"
			android:textSize="24sp" android:gravity="center_horizontal"></TextView>
	</RelativeLayout>  
</FrameLayout>

完全可以写成如下的形式,效果是一样的:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="@+id/frameLayout1"
	android:layout_width="fill_parent" android:layout_height="fill_parent"
	xmlns:android="http://schemas.android.com/apk/res/android">

	<com.gao.apidemo.snake.SnakeView
		android:id="@+id/snake" android:layout_width="fill_parent"
		android:layout_height="fill_parent" tileSize="24" />
	<TextView android:layout_height="fill_parent" android:id="@+id/textView"
			android:gravity="center" android:layout_width="fill_parent"
			android:text="TextView" android:textColor="#ff8888ff"
			android:textSize="24sp" />
</FrameLayout>

可以看到这个textview是覆盖在上面了,上层的TextView对下层的View完全是透明的,而且可以照常的点击,给人的感觉就是加了一个现实文字的透明的图层一样,对下层没有影响。

转载:http://gundumw100.iteye.com/blog/1059685  使用FrameLayout实现遮罩层

利用FrameLayout的特性,可以实现一个简单的遮罩层.


<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <LinearLayout
		    android:orientation="vertical"
		    android:layout_width="fill_parent"
		    android:layout_height="fill_parent"
		    >
	        <Button
	           android:id="@+id/btn"
	           android:layout_width="wrap_content"
	           android:layout_height="wrap_content"
	           android:layout_alignParentRight="true"
	           android:text="show"
	           />
			<TextView  
			    android:layout_width="fill_parent" 
			    android:layout_height="wrap_content" 
			    android:text="@string/hello"
			    />
			<EditText android:layout_width="fill_parent" 
			    android:layout_height="wrap_content" 
			    android:text="Mask"
			    />
		</LinearLayout>
</FrameLayout>
package com.ql.app;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

public class App extends Activity {

	private boolean isMask = true;

	private FrameLayout layout = null;
	private Button btn = null;
	private TextView textView = null;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);

		initViews();
	}

	private void initViews() {

		layout = (FrameLayout) findViewById(R.id.layout);
		btn = (Button) findViewById(R.id.btn);
		btn.setOnClickListener(new MaskListener());

	}

	// 按钮监听,显示/隐藏遮罩
	private class MaskListener implements OnClickListener {
		public void onClick(View v) {
			if (isMask) {
				if(textView==null){
					textView = new TextView(App.this);
					textView.setTextColor(Color.BLUE);
					textView.setTextSize(20);
					textView.setText("I am a mask.");
					textView.setGravity(Gravity.CENTER);
					textView.setLayoutParams(new ViewGroup.LayoutParams(
							ViewGroup.LayoutParams.FILL_PARENT,
							ViewGroup.LayoutParams.FILL_PARENT));
					textView.setBackgroundColor(Color.parseColor("#33FFFFFF"));
				}
				btn.setText("show");
				isMask = false;
				layout.addView(textView);
			} else {
				btn.setText("hide");
				isMask = true;
				layout.removeView(textView);
			}
		}
	}
}

备注一:

看了这几个例子给我的感觉就是FrameLayout主要用在层与层之间的重叠布局,一个布局在另一个布局之上的什么位置

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值