先来看官方文档的定义:FrameLayout是最简单的一个布局对象。它被定制为你屏幕上的一个空白备用区域,以后你能够在其中填充一个单一对象 — 好比,一张你要发布的图片。全部的子元素将会固定在屏幕的左上角;你不能为FrameLayout中的一个子元素指定一个位置。后一个子元素将会直接在前 一个子元素之上进行覆盖填充,把它们部份或所有挡住(除非后一个子元素是透明的)。 java
<?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>
<?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://schemas.android.com/apk/res/android"> <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" /> </merge>
<?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"> <include android:id="@+id/layout1" layout="@layout/relative" /> <include android:id="@+id/layout2" layout="@layout/relative" /> <include android:id="@+id/layout3" layout="@layout/relative" /> </LinearLayout>
<ViewStub android:id="@+id/stub" android:inflatedId="@+id/subTree" android:layout="@layout/mySubTree" android:layout_width="120dip" android:layout_height="40dip" />
ViewStub stub = (ViewStub) findViewById(R.id.stub); View inflated = stub.inflate();
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.grid_2); GridView g = (GridView) findViewById(R.id.myGrid); g.setAdapter(new ImageAdapter(this)); }
public class ImageAdapter extends BaseAdapter { public ImageAdapter(Context c) { mContext = c; } public int getCount() { return mThumbIds.length; } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) { imageView = new ImageView(mContext); imageView.setLayoutParams(new GridView.LayoutParams(45, 45));//设置ImageView宽高 imageView.setAdjustViewBounds(false); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setPadding(8, 8, 8, 8); } else { imageView = (ImageView) convertView; } imageView.setImageResource(mThumbIds[position]); return imageView; } private Context mContext; private Integer[] mThumbIds = { R.drawable.sample_thumb_0, R.drawable.sample_thumb_1, R.drawable.sample_thumb_2, R.drawable.sample_thumb_3, R.drawable.sample_thumb_4, R.drawable.sample_thumb_5, R.drawable.sample_thumb_6, R.drawable.sample_thumb_7 }; }
<?xml version="1.0" encoding="utf-8"?> <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/myGrid" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" android:verticalSpacing="10dp" android:horizontalSpacing="10dp" android:numColumns="auto_fit" android:columnWidth="60dp" android:stretchMode="columnWidth" android:gravity="center" />
//得到Bitmap的高和宽 int bmpWidth=bmp.getWidth(); int bmpHeight=bmp.getHeight(); //设置缩小比例 double scale=0.8; //计算出此次要缩小的比例 scaleWidth=(float)(scaleWidth*scale); scaleHeight=(float)(scaleHeight*scale); //产生resize后的Bitmap对象 Matrix matrix=new Matrix(); matrix.postScale(scaleWidth, scaleHeight); Bitmap resizeBmp=Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, true);
<ImageView android:id="@+id/image" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="center" android:src="@drawable/candle" />
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TableRow> <TextView android:text="column1" android:padding="3dip" /> <TextView android:text="column2" android:padding="3dip" /> <TextView android:text="column3" android:padding="3dip" /> </TableRow> <TableRow> <TextView android:text="column11" android:visibility="invisible"/> //cell不见了 <TextView android:text="左边的invisible" android:gravity="right" android:padding="3dip" /> <Button android:id="@+id/go" android:text="go" android:padding="3dip" /> <Button android:text="cancel" android:padding="3dip" /> </TableRow> <View //间隔线 android:layout_height="2dip" android:background="#F00" /> <TableRow> <TextView android:text="右边的cell empty" /> <TextView android:layout_column="2" android:text="跳开empty cell" android:padding="3dip" /> </TableRow> <TableRow> <TextView android:text="合并3个单元格" android:layout_span="3" android:gravity="center_horizontal" android:background="#FFC0C0C0" android:textColor="#f00" android:padding="3dip" /> </TableRow> </TableLayout>
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:shrinkColumns="0" > // 设置第一个column可收缩 <TableRow> <TextView android:text="column1" android:padding="3dip" /> <TextView android:text="column2" android:padding="3dip" /> <TextView android:text="column3" android:padding="3dip" /> </TableRow> <TableRow> <TextView android:text="column11" android:visibility="invisible"/> <TextView android:text="左边的invisible" android:gravity="right" android:padding="3dip" /> <Button android:id="@+id/go2" android:text="go2" android:padding="3dip" /> <Button android:text="cancel" android:padding="3dip" /> </TableRow> <View android:layout_height="2dip" android:background="#F00" /> <TableRow> <TextView android:text="右边的cell empty" /> <TextView android:layout_column="2" android:text="跳开empty cell" android:padding="3dip" /> <TextView android:text="123456789" android:padding="3dip" /> </TableRow> </TableLayout>
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchColumns="1"> // 设置第二个column可伸展 <TableRow> <TextView android:text="column1" android:padding="3dip" /> <TextView android:text="column2" android:gravity="right" android:padding="3dip" /> <TextView android:text="column3" android:padding="3dip" /> </TableRow> <TableRow> <TextView android:text="column1" android:padding="3dip" /> <TextView android:text="column2" android:gravity="right" android:padding="3dip" /> <TextView android:text="column3" android:padding="3dip" /> </TableRow> </TableLayout>
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchColumns="1"> <TableRow> <TextView android:text="@string/table_layout_10_user" android:textStyle="bold" android:gravity="right" android:padding="3dip" /> <EditText android:id="@+id/username" android:text="@string/table_layout_10_username_text" android:padding="3dip" android:scrollHorizontally="true" /> </TableRow> <TableRow> <TextView android:text="@string/table_layout_10_password" android:textStyle="bold" android:gravity="right" android:padding="3dip" /> <EditText android:id="@+id/password" android:text="@string/table_layout_10_password_text" android:password="true" android:padding="3dip" android:scrollHorizontally="true" /> </TableRow> <TableRow android:gravity="right"> <Button android:id="@+id/cancel" android:text="@string/table_layout_10_cancel" /> <Button android:id="@+id/login" android:text="@string/table_layout_10_login" /> </TableRow> </TableLayout>
public class Tabs1 extends TabActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TabHost tabHost = getTabHost(); LayoutInflater.from(this).inflate(R.layout.tabs1, tabHost.getTabContentView(), true); tabHost.addTab(tabHost.newTabSpec("tab1") .setIndicator("tab1") .setContent(R.id.view1)); tabHost.addTab(tabHost.newTabSpec("tab3") .setIndicator("tab2") .setContent(R.id.view2)); tabHost.addTab(tabHost.newTabSpec("tab3") .setIndicator("tab3") .setContent(R.id.view3)); } }
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/view1" android:background="@drawable/blue" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="@string/tabs_1_tab_1"/> <TextView android:id="@+id/view2" android:background="@drawable/red" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="@string/tabs_1_tab_2"/> <TextView android:id="@+id/view3" android:background="@drawable/green" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="@string/tabs_1_tab_3"/> </FrameLayout> <! -- strings.xml <string name="tabs_1_tab_1">tab1</string> <string name="tabs_1_tab_2">tab2</string> <string name="tabs_1_tab_3">tab3</string> -->原来是用FrameLayout的!
public class Tabs2 extends TabActivity implements TabHost.TabContentFactory { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final TabHost tabHost = getTabHost(); tabHost.addTab(tabHost.newTabSpec("tab1") .setIndicator("tab1", getResources().getDrawable(R.drawable.star_big_on)) .setContent(this)); tabHost.addTab(tabHost.newTabSpec("tab2") .setIndicator("tab2") .setContent(this)); tabHost.addTab(tabHost.newTabSpec("tab3") .setIndicator("tab3") .setContent(this)); } public View createTabContent(String tag) { final TextView tv = new TextView(this); tv.setText("Content for tab with tag " + tag); return tv; } }
public class Tabs3 extends TabActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final TabHost tabHost = getTabHost(); tabHost.addTab(tabHost.newTabSpec("tab1") .setIndicator("list") .setContent(new Intent(this, List1.class))); tabHost.addTab(tabHost.newTabSpec("tab2") .setIndicator("photo list") .setContent(new Intent(this, List8.class))); // This tab sets the intent flag so that it is recreated each time // the tab is clicked. tabHost.addTab(tabHost.newTabSpec("tab3") .setIndicator("destroy") .setContent(new Intent(this, Controls2.class) .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))); } }