View.VISIBLE--->可见
View.INVISIBLE--->不可见,但这个View仍然会占用在xml文件中所分配的布局空间,不重新layout。
View.GONE---->不可见,但这个View在ViewGroup中不保留位置,会重新layout,不再占用空间,那后面的view就会取代他的位置。
[java] view plain copy package cn.zyh.l009layout;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Path.FillType;
import android.text.Layout;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends Activity
{
private LinearLayout layout;
private View.OnClickListener removeListener; @Override protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = (LinearLayout) findViewById(R.id.TableLayout1);
removeListener = new View.OnClickListener()
{ @Override public void onClick(View v)
{ layout.removeView(v); } };
for (int i = 0; i < 5; i++)
{ Button btn = new Button(this); btn.setText("Remove me" + i); // width 和 height , -1 fill_parent ,-2 wrap_content // http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html#FILL_PARENT
layout.addView(btn, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); btn.setOnClickListener(removeListener);
}
}
@Override public boolean onCreateOptionsMenu(Menu menu)
{ // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}