Android View中 layout 的使用
2014-02-25 15:38
405 查看
1、layout简介
给View 以及其它的所有的子节点都分配了一个尺寸;这也是布局的第二个步骤,(第一个步骤是measuing).在这一个步骤中,每一个每一个父节点都会回调layout.This is typically done using the child measurements
that were stored in the measure pass()
继承的子类不应当overide这个方法。而是应当继承onlayout这个方法.在这个onlayout这个方法中,他们应当嗲用layout方法,遍历每一个子节点的布局。
(我们可以查看AbsoluteLayout 的code)
2、layout用法一:
我们可以看下 Android 仿美团网,大众点评购买框悬浮效果之修改版其,在ScrollView中,对layout使用的思想是比较ok的。
** * @blog http://blog.csdn.net/xiaanming * * @author xiaanming * */ public class MTActivity extends Activity implements OnScrollListener{ /** * 自定义的MyScrollView */ private MTScrollView myScrollView; /** * 在MyScrollView里面的购买布局 */ private LinearLayout mBuyLayout; /** * 位于顶部的购买布局 */ private LinearLayout mTopBuyLayout; @SuppressWarnings("deprecation") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mt_activity_main); myScrollView = (MTScrollView) findViewById(R.id.scrollView); mBuyLayout = (LinearLayout) findViewById(R.id.buy); mTopBuyLayout = (LinearLayout) findViewById(R.id.top_buy_layout); myScrollView.setOnScrollListener(this); // mTopBuyLayout.setV //当布局的状态或者控件的可见性发生改变回调的接口 // 当界面初始化的时候,就对界面上的可移动的买的布局 进行一移动到指定的位置 findViewById(R.id.parent_layout).getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { //这一步很重要,使得上面的购买布局和下面的购买布局重合 System.out.println("onGlobalLayout"+myScrollView.getScrollY()); onScroll(myScrollView.getScrollY()); // System.out.println(myScrollView.getScrollY()); } }); } /** * 将那个买的在fragment中 可以移动的那个买的布局, * 不停的移动那个布局,当可移动的那个布局到达了顶部的时候, 就不在移动那个可以移动的布局 * * * */ @Override public void onScroll(int scrollY) { int mBuyLayout2ParentTop = Math.max(scrollY, mBuyLayout.getTop()); System.out.println("mBuyLayout2ParentTop "+mBuyLayout2ParentTop); mTopBuyLayout.layout(0, mBuyLayout2ParentTop, mTopBuyLayout.getWidth(), mBuyLayout2ParentTop + mTopBuyLayout.getHeight()); } }
/** * @blog http://blog.csdn.net/xiaanming * * @author xiaanming * */ public class MTScrollView extends ScrollView{ private OnScrollListener onScrollListener; public MTScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } public MTScrollView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } public MTScrollView(Context context) { super(context); // TODO Auto-generated constructor stub } /** * 设置滚动接口 * @param onScrollListener */ public void setOnScrollListener(OnScrollListener onScrollListener) { this.onScrollListener = onScrollListener; } @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { super.onScrollChanged(l, t, oldl, oldt); if(onScrollListener != null){ System.out.println("onScrollChanged"); onScrollListener.onScroll(t); } } /** * * 滚动的回调接口 * * @author xiaanming * */ public interface OnScrollListener{ /** * 回调方法, 返回MyScrollView滑动的Y方向距离 * @param scrollY * 、 */ public void onScroll(int scrollY); } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/parent_layout" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="45dip" android:scaleType="centerCrop" android:src="@drawable/navigation_bar" /> <com.ownku.view.MTScrollView android:id="@+id/scrollView" android:layout_width="fill_parent" android:layout_height="fill_parent" > <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android:id="@+id/iamge" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/pic" android:scaleType="centerCrop" /> <include android:id="@+id/buy" layout="@layout/buy_layout" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/one" android:scaleType="centerCrop" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/one" android:scaleType="centerCrop" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/one" android:scaleType="centerCrop" /> </LinearLayout> <include android:id="@+id/top_buy_layout" layout="@layout/buy_layout" /> </FrameLayout> </com.ownku.view.MTScrollView> </LinearLayout>
当买东西的那个布局移动开始保持在布局中的buy_layout的布局离顶部的高度保持一致,以达到对其在LinearLayout时,其中buy_layout可见时候,对其的覆盖,当其移动走,不可见的时候。 top_buy_layout就停留在其顶部,通过layout数的变化。
3、layout用法三
其中的,layout的移动是相对于父类给子类提供的空间进行移动的。点击上面的button 对img 进行移动,图上的移动是相对于图片所在父类的空间进行的移动。
ImageView img; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); img=(ImageView) findViewById(R.id.imageView1); findViewById(R.id.button1).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // animation2(); lay1(); } }); } protected void lay1() { // TODO Auto-generated method stub /** * 是相对于改view 所在的父类的布局,所发生的变化的位移 * */ img.layout(100 , 100, 310, 150); }
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <LinearLayout android:layout_width="300dp" android:layout_height="300dp" android:layout_centerInParent="true" android:background="@android:color/darker_gray" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> </LinearLayout> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" android:text="Button" /> </RelativeLayout>
相关文章推荐
- (原)android修改文件所属的用户组
- 定位android kernel crash的方式
- 关于Android SDK无法更新的解决办法
- Android中线程通讯类Handler
- android ndk调试工具
- 关于Android SDK无法更新的解决办法
- android dolby
- Android NDK的入门学习过程(安装和helloJNI例程)
- Android入门第十五篇之ActivityGroup + GridView 实现Tab分页标签
- android 之 DataPickerDialog简单应用
- android中:/system/bin/sh: : No such file or directory错误
- Android记录自己的日志
- Android APK反编译详解
- Android "R cannot be resolved to a variable" 解决方法
- android 线程
- Android System.out.println在控制台无输出
- Android找不到Manifest系统文件
- android.os.NetworkOnMainThreadException异常处理
- android自动连接指定wifi
- android中的国际化