Android 常用布局视图

# ScrollView 滚动视图
<ScrollView
	android:layout_width="wrap_content"
	android:layout_height="wrap_content">
	<HorizontalScrollView
		android:layout_width="wrap_content"
		android:layout_height="wrap_content">
		<Button
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:width="10000dp"
			android:height="10000dp"
			/>
	</HorizontalScrollView>
</ScrollView>

# TabHost
	1. 静态
	主XML:
		<TabHost
			android:id="@+id/tabhost"
			android:layout_width="match_parent"
			android:layout_height="wrap_content">
			<LinearLayout
				android:layout_width="match_parent"
				android:layout_height="match_parent"
				android:orientation="vertical" >
				<TabWidget
					android:id="@android:id/tabs"
					android:layout_width="match_parent"
					android:layout_height="wrap_content" >
				</TabWidget>
				<FrameLayout
					android:id="@android:id/tabcontent"
					android:layout_width="match_parent"
					android:layout_height="match_parent" >
					<LinearLayout
						android:id="@+id/tab1"
						android:layout_width="match_parent"
						android:layout_height="match_parent" >
						<TextView
							android:id="@+id/textView1"
							android:layout_width="wrap_content"
							android:layout_height="wrap_content"
							android:text="林炳东" />
					</LinearLayout>
				</FrameLayout>
			</LinearLayout>
		</TabHost>
	TAB XML: xxx.xml
		<?xml version="1.0" encoding="utf-8"?>
		<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
			android:orientation="vertical" android:layout_width="match_parent"
			android:layout_height="match_parent"
			android:id="@+id/xxx">
			<Button
				style="?android:attr/buttonStyleSmall"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"
				android:text="New Button"
				android:id="@+id/button"
				android:layout_gravity="center_horizontal" />
		</LinearLayout>
	JAVA:
		TabHost th=(TabHost)findViewById(R.id.tabhost);
        th.setup();
        LayoutInflater i= LayoutInflater.from(this);
        i.inflate(R.layout.xxx, th.getTabContentView());
        th.addTab(th.newTabSpec("tab1")
                .setIndicator("标签1", null)
                .setContent(R.id.tab1));
        th.addTab(th.newTabSpec("tab2")
                .setIndicator("标签2", null)
                .setContent(R.id.xxx));
	动态内容:
		TabHost th=(TabHost)findViewById(R.id.tabHost);
        th.setup();
        TabHost.TabSpec tabSpec = th.newTabSpec("tab1")
                .setIndicator("标签1", null)
                .setContent(new TabHost.TabContentFactory() {
                    @Override
                    public View createTabContent(String tag) {
                        TextView text = new TextView(tabactivity.this);
                        text.setText("text1");
                        return text;
                    }
                });
        th.addTab(tabSpec);
        tabSpec = th.newTabSpec("tab2")
                .setIndicator("标签2", null)
                .setContent(new TabHost.TabContentFactory() {
                    @Override
                    public View createTabContent(String tag) {
                        TextView text = new TextView(tabactivity.this);
                        text.setText("text2");
                        return text;
                    }
                });
        th.addTab(tabSpec);

# ViewStub 延时加载视图
	<ViewStub
        android:id="@+id/rload"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout="@layout/xxx"
        />

	ViewStub vs = (ViewStub) findViewById(R.id.rload);
    vs.inflate();

# ImageSwitcher [类似 ViewSwitcher TextSwitcher]
	XML:
		<ImageSwitcher
            android:id="@+id/imageSwitcher"
            android:layout_marginTop="5dp"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"   >
        </ImageSwitcher>
        <Button
            android:id="@+id/change"
            android:text="change"
            android:layout_marginLeft="30dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ></Button>
	JAVA:
		private Integer[] mImageIds = { R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4};
		private  int i=0;
		private float offp=0;
		@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.tab);

			final ImageSwitcher img = (ImageSwitcher) findViewById(R.id.imageSwitcher);
			//显示VIEW
			img.setFactory(new ViewSwitcher.ViewFactory() {
				@Override
				public View makeView() {
					ImageView i = new ImageView(tabactivity.this);
					i.setBackgroundColor(0xFF000000);
					i.setScaleType(ImageView.ScaleType.FIT_CENTER);
					i.setLayoutParams(new ImageSwitcher.LayoutParams(
							ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
					return i;
				}
			});
			img.setImageDrawable(getDrawable(mImageIds[2]));
			img.setOnTouchListener(new View.OnTouchListener() {
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					//getRawX()和getRawY()获得的是相对屏幕的位置
					//getX()和getY()获得的永远是相对view的触摸位置坐标
					//返回 false 将不会触发其他事件
					Log.i("MontionEvent",String.valueOf(event.getAction()));
					switch (event.getAction()) {
						case MotionEvent.ACTION_DOWN:
							offp = event.getX();
							break;
						case MotionEvent.ACTION_MOVE:
						 //   img.setLeft(-(int)(offp - event.getX()));
							break;
						case MotionEvent.ACTION_UP:
							if (offp - event.getX() > 10) {
								i--;
								img.setInAnimation(AnimationUtils
										.loadAnimation(tabactivity.this,
												R.anim.slide_in_right));
								img.setOutAnimation(AnimationUtils
										.loadAnimation(tabactivity.this,
												R.anim.slide_out_left));
							} else if (offp - event.getX() < 10) {
								i++;
								img.setInAnimation(AnimationUtils.loadAnimation(tabactivity.this,
										android.R.anim.slide_in_left));
								img.setOutAnimation(AnimationUtils.loadAnimation(tabactivity.this,
										android.R.anim.slide_out_right));
							}else
								return true;
							if (i < 0) i = mImageIds.length;
							img.setImageDrawable(getDrawable(mImageIds[i % mImageIds.length]));
							break;
					}

					return true;
				}
			});
			Button but = (Button) findViewById(R.id.change);
			but.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					i++;
					img.setInAnimation(AnimationUtils.loadAnimation(tabactivity.this,
							android.R.anim.slide_in_left));
					img.setOutAnimation(AnimationUtils.loadAnimation(tabactivity.this,
							android.R.anim.slide_out_right));
					img.setImageDrawable(getDrawable(mImageIds[i % mImageIds.length]));
				}
			});
		}

# ViewFlipper 带自动播放的 ViewSwitcher
	XML :
		 <ViewFlipper
			android:layout_width="match_parent"
			android:layout_height="100dp"
			android:id="@+id/filp">
			<!-- 第一个页面 -->
			<LinearLayout android:layout_width="fill_parent"
				android:layout_height="fill_parent" android:gravity="center">
				<ImageView android:layout_width="wrap_content"
					android:layout_height="wrap_content" android:src="@drawable/a1" />
			</LinearLayout>
			<!-- 第二个页面 -->
			<LinearLayout android:layout_width="fill_parent"
				android:layout_height="fill_parent" android:gravity="center">
				<ImageView android:layout_width="wrap_content"
					android:layout_height="wrap_content" android:src="@drawable/a2"
					android:gravity="center" />
			</LinearLayout>
			<!-- 第三个页面 -->
			<LinearLayout android:layout_width="fill_parent"
				android:layout_height="fill_parent" android:gravity="center">

				<ImageView android:layout_width="wrap_content"
					android:layout_height="wrap_content" android:src="@drawable/a3"
					android:gravity="center" />
			</LinearLayout>
			<!-- 第四个页面 -->
			<LinearLayout android:layout_width="fill_parent"
				android:layout_height="fill_parent" android:gravity="center">
				<ImageView android:layout_width="wrap_content"
					android:layout_height="wrap_content" android:src="@drawable/a4"
					android:gravity="center" />
			</LinearLayout>
		</ViewFlipper>
		<Button
			android:id="@+id/fc"
			android:text="change"
			android:layout_marginLeft="30dp"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			></Button>
	JAVA:
		final ViewFlipper viewFlipper = (ViewFlipper) findViewById(R.id.filp);
        viewFlipper.setInAnimation(AnimationUtils.loadAnimation(tabactivity.this,
                android.R.anim.slide_in_left));
        viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(tabactivity.this,
                android.R.anim.slide_out_right));
        Button fc = (Button) findViewById(R.id.fc);
        fc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(viewFlipper.isFlipping())
                    viewFlipper.stopFlipping();
                else
                    viewFlipper.startFlipping();
            }
        });
        viewFlipper.startFlipping();

# ViewPager 左右滚动页面
	XML:
		<android.support.v4.view.ViewPager
			android:layout_width="match_parent"
			android:layout_height="1000dp"
			android:id="@+id/viewpager">
			<!-- PagerTabStrip 标题底部线 -->
			<android.support.v4.view.PagerTabStrip
				android:id="@+id/tabstrip"
				android:layout_width="wrap_content"
				android:layout_height="50dip"
				android:gravity="center" />
		</android.support.v4.view.ViewPager>
	JAVA:
		ViewPager pager = null;
		PagerTabStrip tabStrip = null;
		ArrayList<View> viewContainter = new ArrayList<View>();
		ArrayList<String> titleContainer = new ArrayList<String>();
		public String TAG = "tag";
		@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.tab);
			pager = (ViewPager) this.findViewById(R.id.viewpager);
			tabStrip = (PagerTabStrip) this.findViewById(R.id.tabstrip);
			//取消tab下面的长横线
			//tabStrip.setDrawFullUnderline(false);
			//设置当前tab页签的下划线颜色
			tabStrip.setTabIndicatorColor(this.getResources().getColor(android.R.color.holo_blue_bright));
			tabStrip.setTextSpacing(200);

			View view1 = LayoutInflater.from(this).inflate(R.layout.xxx, null);
			View view2 = LayoutInflater.from(this).inflate(R.layout.xxx, null);

			//viewpager开始添加view
			viewContainter.add(view1);
			viewContainter.add(view2);

			//页签项
			titleContainer.add("网易新闻");
			titleContainer.add("网易体育");

			pager.setAdapter(new PagerAdapter() {
				@Override
				public int getCount() {
					return viewContainter.size();
				}

				@Override
				public void destroyItem(ViewGroup container, int position,
										Object object) {
					((ViewPager) container).removeView(viewContainter.get(position));
				}

				//每次滑动的时候生成的组件
				@Override
				public Object instantiateItem(ViewGroup container, int position) {
					((ViewPager) container).addView(viewContainter.get(position));
					return viewContainter.get(position);
				}

				@Override
				public boolean isViewFromObject(View arg0, Object arg1) {
					return arg0 == arg1;
				}

				@Override
				public int getItemPosition(Object object) {
					return super.getItemPosition(object);
				}

				@Override
				public CharSequence getPageTitle(int position) {
					return titleContainer.get(position);
				}
			});
			pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
				@Override
				public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

				}

				@Override
				public void onPageSelected(int position) {

				}

				@Override
				public void onPageScrollStateChanged(int state) {

				}
			});

		}
	配合 Fragment 使用
	XML:
		 <android.support.v4.view.ViewPager
			android:layout_width="match_parent"
			android:layout_height="1000dp"
			android:id="@+id/viewPager">
			<android.support.v4.view.PagerTabStrip
				android:id="@+id/tabstrip"
				android:layout_width="wrap_content"
				android:layout_height="50dip"
				android:gravity="center" />
		</android.support.v4.view.ViewPager>
	JAVA:
		 List<Fragment> fragmentList = new ArrayList<Fragment>();
    List<String>   titleList    = new ArrayList<String>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab);
        ViewPager vp = (ViewPager)findViewById(R.id.viewPager);
        fragmentList.add(new ViewPagerFragment1("页面1"));
        fragmentList.add(new ViewPagerFragment1("页面2"));
        fragmentList.add(new ViewPagerFragment1("页面3"));
        titleList.add("title 1 ");
        titleList.add("title 2 ");
        titleList.add("title 3 ");
        vp.setAdapter(new myPagerAdapter(getSupportFragmentManager(), fragmentList, titleList));
    }
    class myPagerAdapter extends FragmentPagerAdapter {
        private List<Fragment> fragmentList;
        private List<String>   titleList;
        public myPagerAdapter(FragmentManager fm, List<Fragment> fragmentList, List<String> titleList){
            super(fm);
            this.fragmentList = fragmentList;
            this.titleList = titleList;
        }
        @Override
        public Fragment getItem(int arg0) {
            return (fragmentList == null || fragmentList.size() == 0) ? null : fragmentList.get(arg0);
        }
        @Override
        public CharSequence getPageTitle(int position) {
            return (titleList.size() > position) ? titleList.get(position) : "";
        }
        @Override
        public int getCount() {
            return fragmentList == null ? 0 : fragmentList.size();
        }
    }
    public class ViewPagerFragment1 extends Fragment {
        public ViewPagerFragment1(String text){
            super();
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.xxx, container, false);
            return v;
        }
    }

# SwipeRefreshLayout 下拉刷新
	XML
		<android.support.v4.widget.SwipeRefreshLayout
			android:id="@+id/swipe_container"
			android:layout_width="match_parent"
			android:layout_height="match_parent" >
			<!--不用ScrollView 会导致加载图标被覆盖-->
			<ScrollView
				android:layout_width="match_parent"
				android:layout_height="wrap_content" >
				<TextView
					android:id="@+id/textView1"
					android:layout_width="match_parent"
					android:layout_height="wrap_content"
					android:gravity="center"
					android:paddingTop="10dp"
					android:text="zzzzzzzzz"
					android:textSize="20sp"
					android:textStyle="bold" />
			</ScrollView>
		</android.support.v4.widget.SwipeRefreshLayout>
	JAVA:
		final SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swipe_container);
        //设置刷新时动画的颜色,可以设置4个
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Log.i("SwipeRefreshLayout","REFRESH");
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Log.i("SwipeRefreshLayout", "REFRESH OK");
                        swipeRefreshLayout.setRefreshing(false);
                    }
                }, 6000);
            }
        });

带上拉加载的 SwipeRefreshLayout
	compile ‘com.demievil.library:refreshlayout:[email protected]‘

SlidingPaneLayout 左右面板 [右边移动]
	XML:
		<android.support.v4.widget.SlidingPaneLayout xmlns:android="http://schemas.android.com/apk/res/android"
			xmlns:tools="http://schemas.android.com/tools"
			android:id="@+id/slidingpanellayout"
			android:layout_width="match_parent"
			android:layout_height="match_parent"
			tools:context=".MainActivity" >
			<fragment
				android:id="@+id/leftfragment"
				android:name="com.example.dome2.tabactivity$BookMarkerFragment"
				android:layout_width="100dp"
				android:layout_height="match_parent"
				android:layout_gravity="left" />
			<fragment
				android:id="@+id/rightfragment"
				android:name="com.example.dome2.tabactivity$BookMarkerFragment2"
				android:layout_width="match_parent"
				android:layout_height="match_parent"
				android:layout_gravity="right"
				android:layout_weight="1" />
		</android.support.v4.widget.SlidingPaneLayout>
	JAVA:
		 @Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.tab);

			SlidingPaneLayout spl = (SlidingPaneLayout) this.findViewById(R.id.slidingpanellayout);
			spl.setPanelSlideListener(new SlidingPaneLayout.PanelSlideListener() {
				@Override
				public void onPanelClosed(View view) {
					//面板打开
				}
				@Override
				public void onPanelOpened(View viw) {
					//面板关闭
				}
				@Override
				public void onPanelSlide(View arg0, float arg1) {

				}
			});
		}
		public static class BookMarkerFragment extends Fragment {
			@Override
			public View onCreateView(LayoutInflater inflater, ViewGroup container,
									 Bundle savedInstanceState) {
				View view = inflater.inflate(R.layout.xxx, container, false);
				return view;
			}
		}
		public static class BookMarkerFragment2 extends Fragment {
			@Override
			public View onCreateView(LayoutInflater inflater, ViewGroup container,
									 Bundle savedInstanceState) {
				View view = inflater.inflate(R.layout.xxx, container, false);
				return view;
			}
		}

DrawerLayout 左右面板 [右边固定]
	XML:
		<android.support.v4.widget.DrawerLayout
			android:id="@+id/drawer_layout"
			android:layout_width="match_parent"
			android:layout_height="match_parent" >

			<!-- The main content view -->

			<FrameLayout
				android:id="@+id/content_frame"
				android:layout_width="match_parent"
				android:layout_height="match_parent" >

				<Button
					android:id="@+id/btn"
					android:layout_width="match_parent"
					android:layout_height="wrap_content"
					android:text="open"
					/>
			</FrameLayout>

			<!-- The navigation drawer -->

			<ListView
				android:id="@+id/left_drawer"
				android:layout_width="240dp"
				android:layout_height="match_parent"
				android:layout_gravity="start"
				android:background="#111"
				android:choiceMode="singleChoice"
				android:divider="@android:color/transparent"
				android:dividerHeight="0dp" />
		</android.support.v4.widget.DrawerLayout>
	JAVA:
		final DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        Button button = (Button) findViewById(R.id.btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 按钮按下,将抽屉打开
                mDrawerLayout.openDrawer(Gravity.LEFT);

            }
        });

FrameLayout 层叠显示

LinearLayout 线性排列

TableLayout TableRow 表格排列

GridLayout 格子

RelativeLayout 相对排列

LinearLayoutCompat 线性布局 每个组件间加 divider
	XML:
		<android.support.v7.widget.LinearLayoutCompat
			xmlns:android="http://schemas.android.com/apk/res/android"
			xmlns:app="http://schemas.android.com/apk/res-auto"
			android:layout_width="match_parent"
			android:layout_height="match_parent"
			android:padding="20dip"
			android:orientation="vertical"
			app:divider="@drawable/line"
			app:dividerPadding="5dp"
			app:showDividers="beginning|middle|end" >

			<TextView
				android:layout_width="match_parent"
				android:layout_height="wrap_content"
				android:gravity="center"
				android:text="CSDN Zhang Phil" />
			<TextView
				android:layout_width="match_parent"
				android:layout_height="wrap_content"
				android:gravity="center"
				android:text="CSDN Zhang Phil" />
			<TextView
				android:layout_width="match_parent"
				android:layout_height="wrap_content"
				android:gravity="center"
				android:text="CSDN Zhang Phil" />
		</android.support.v7.widget.LinearLayoutCompat>

  

时间: 2024-10-13 12:00:20

Android 常用布局视图的相关文章

Android常用布局类整理(一)

Android常用布局类整理 最近又回头做了一下android的项目,发觉越来越不从心,很多东西都忘了,简单的页面布局也很多写不出来,首先还是先整理一下一些会混淆的概念先 layout_width/layout_height的两种不同的方式 ① wrap_content能包裹其中的内容即可 ② fill_parent/match_parent 填满父视图的空间 LinearLayout 按垂直(vertical)或水平(horizontal)对齐每一个子视图,它包含的子控件将以横向或竖向的方式排

Android常用布局和控件

一.Android常用布局属性 1. LinearLayout的特有属性 android:orientation:设置布局排列方式   android:layout_weight:设置所占布局的权重   android:weightSum:设置最大权重和 2. RelativeLayout的特有属性 属性值为“true”或“false”android:layout_centerHrizontal:位于父控件的横向中间位置android:layout_centerVertical:位于父控件的纵向

android常用布局

一.线性布局LinearLayout 作用:将容器中的组件一个挨一个地排列起来,不仅可以控制各组件横向排列也可以控制各组件纵向排列(通过android:orientation属性控制) 特点:线性布局不会换行当组件一个爱一个地排列到头之后剩余的组件不会显示 属性: android:gravity   设置布局管理器内组件的对齐方式,该属性支持top,bottom,left,right,center_vertical,fill_vertical,center_horizontal,fill_hor

【学习笔记】Android常用布局

一.LinearLayout线性布局 (1)线性布局分为垂直和水平两个方向      android:orientation="vertical"      android:orientation="horizontal" (2)定义宽和高      铺满父级容器:match_parent      根据内容自适应:wrap_content      android:layout_width=""      android:layout_heig

Android仿微信UI布局视图(圆角布局的实现)

圆角按钮,或布局可以在xml文件中实现,但也可以使用图片直接达到所需的效果,以前版本的微信就使用了这种方法. 实现效果图:    不得不说,这种做法还是比较方便的. 源代码: MainActivity(没写任何代码,效果全在布局文件中实现): package com.android_settings; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity

Android常用界面布局(二)

ImageView ScaleType属性, 该属性用以表示显示图片的方式 ①matrix               根据一个3x3的矩阵对其中图片进行缩放 ②fitXY                  将图片非等比例缩放到大小与ImageView相同 ③fitStart               缩放方式同FIT_CENTER,只是将图片显示在左方或上方,而不是居中 ④fitCenter           ImageView的默认状态,大图等比例缩小,小图等比例放大,整体居中显示在Im

麦子学院android开发之Android应用开发视图优化步骤

1)View优化 i.   减少不必要的View以及View的嵌套层次. 比如实现一个listview中常用的layout,可以使用RelativeLayout减少嵌套,要知道每个View的对象会耗费1~2k内存,嵌套层次过多会引起频繁的gc,造成ANR. ii.   通过HierarchyViewer查看布局结构 利用HierarchyViewer来查看View的结构:~/tools/hierarchyviewer,能很清楚地看到RelativeLayout下面的扁平结构,这样能加快dom的渲

Android02.常用布局及基本UI控件

一.Android学习API指南:[了解] 1. 应用的组成部分   App Components 1.1. 应用的基本原理    App Fundamentals 1.2. Activity      Activities 1.2.1. 片段    Fragments 1.2.2. 加载器     Loaders 1.2.3. 任务和返回堆    Tasks and Back Stack 1.3. Service服务   Services 1.3.1. 绑定服务     Bound Servi

Android常用酷炫控件(开源项目)github地址汇总

转载一个很牛逼的控件收集贴... 第一部分 个性化控件(View) 主要介绍那些不错个性化的 View,包括 ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.ProgressBar.TextView.ScrollView.TimeView.TipView.FlipView.ColorPickView.GraphView.UI Style 等等. 一.ListView android-pulltorefresh一个强大的拉动