Android布局基础知识之wrap_content与match_parent的区别,(和配合使用layout_weight的不同)

Android布局基础知识之wrap_content与match_parent的区别,(和配合使用layout_weight的不同)

一、定义

match_parent:表示让当前控件的大小和父布局的大小一样,即由父布局来决定当前控件的大小。

wrap_content:表示让当前控件的大小刚好能够包含里面的内容,即由控件内容决定当前控件的大小。

二、配合layout_weight的不同

layout_weight:表示线性分割原本应有长度的权重,要和wrap_content和match_parent配合使用。

layout_weight的计算方式:LinearLayout布局中含有weight的子控件时,LinearLayout会measure两次

设屏幕宽度为X,子控件1为x1,子控件2为x2

第一次测量宽度:x1=X, x2=X ,total_width为2X;
(因为用了weight,所以linearLayout每次measure child时不考虑前一个已经占用的大小)

第二次计算delta(偏差):delta=X-total_width=-X, 然后会将x1的宽度设为 X+delta1/3=0.66X, x2的宽度为 X+delta*2/3=0.33X。

试着理解这句话!!
1、layout_width="match_parent"时,
控件的宽度为整个父布局宽度,即match_parent
剩余空间宽度 = 父布局宽度 - 总权重*控件宽度(这里为父布局宽度)
分析:结果为负值,因此比例为反比。

layout_width="wrap_content"时
控件的宽度为内容宽度,而wrap_content表示为0dp
剩余空间宽度 = 父布局宽度 - 总权重*控件宽度(这里为0dp)
分析:结果为正值,因此比例为正比。

通用计算长度公式:长度=控件长度+权重比例*剩余空间大小

Google官方推荐,当使用weight属性时,将width设为0dp即可,效果跟设成wrap_content是一样的。这样weight就可以理解为占比了!

  • 13
    点赞
  • 80
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
粘性控件,其任意一个子控件都可滑动停留,无论是View,还是ViewGroup;用该控件可以轻松实现支付宝"全部应用"界面。演示图  Note:图1为设置属性wkp_canScrollToEndViewTop=true,图2没有;图3为设置滑动改变监听。Gradle集成dependencies{       compile 'com.wkp:StickLayout:1.0.6'       //Android Studio3.0 可用以下方式       //implementation 'com.wkp:StickLayout:1.0.6' } //如不愿意等待,请加上我的maven仓库地址 maven { url "https://dl.bintray.com/wkp/maven" }Note:可能存在Jcenter还在审核阶段,这时会集成失败!注意SDK版本targetSdkVersion >= 26.使用详解属性讲解<!--是否粘性停留(用于直接子控件)-->         <attr name="wkp_stick" format="boolean"/>         <!--是否开启滑动到最后一个控件的顶部,默认不开启(用于控件本身),注意最后一个子控件如果为条目控件时,如ListView,请不要开启-->         <attr name="wkp_canScrollToEndViewTop" format="boolean"/>Note:每个属性都有对应的java设置代码!布局<?xml version="1.0" encoding="utf-8"?> <LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical">     <TextView         android:clickable="true"         android:onClick="addView"         android:gravity="center"         android:padding="5dp"         android:text="添加条目"         android:layout_width="match_parent"         android:layout_height="wrap_content"/>         <!--app:wkp_canScrollToEndViewTop="true"-->     <com.wkp.sticklayout_lib.widget.StickLayout         android:id="@ id/sl"         android:layout_width="match_parent"         android:layout_height="wrap_content">         <TextView             android: id/tv1"             android:text="第1行"             android:gravity="center"             android:layout_width="match_parent"             android:layout_height="200dp"/>         <LinearLayout             app:wkp_stick="true"             android:orientation="horizontal"             android:layout_width="match_parent"             android:layout_height="40dp">             <TextView                 android:onClick="scrollTo"                 android:background="@android:color/holo_blue_light"                 android:text="NUM2"                 android:gravity="center"                 android:layout_weight="1"                 android:layout_width="0dp"                 android:layout_height="match_parent"/>             <TextView                 android:onClick="scrollTo3"                 android:background="@android:color/holo_green_light"                 android:text="NUM3"                 android:gravity="center"                 android:layout_weight="1"                 android:layout_width="0dp"                 android:layout_height="match_parent"/>             <TextView                 android:onClick="scrollTo4"                 android:background="@android:color/holo_red_light"                 android:text="NUM4"                 android:gravity="center"                 android:layout_weight="1"                 android:layout_width="0dp"                 android:layout_height="match_parent"/>             <TextView                 android:onClick="scrollTo7"                 android:background="@android:color/holo_orange_light"                 android:text="NUM7"                 android:gravity="center"                 android:layout_weight="1"                 android:layout_width="0dp"                 android:layout_height="match_parent"/>         </LinearLayout>         <TextView             android:id="@ id/tv2"             android:text="第2行"             android:background="@android:color/holo_blue_light"             android:gravity="center"             android:layout_width="match_parent"             android:layout_height="200dp"/>         <TextView             android:id="@ id/tv3"             app:wkp_stick="true"             android:text="第3行"             android:background="@android:color/holo_green_light"             android:gravity="center"             android:layout_width="match_parent"             android:layout_height="200dp"/>         <TextView             android:background="@android:color/holo_red_light"             android:id="@ id/tv4"             android:text="第4行"             android:gravity="center"             android:layout_width="match_parent"             android:layout_height="200dp"/>         <TextView             android:id="@ id/tv5"             android:text="第5行"             android:gravity="center"             android:layout_width="match_parent"             android:layout_height="200dp"/>         <TextView             android:id="@ id/tv6"             android:text="第6行"             android:gravity="center"             android:layout_width="match_parent"             android:layout_height="200dp"/>         <TextView             android:background="@android:color/holo_orange_light"             android:id="@ id/tv7"             android:text="第7行"             android:gravity="center"             android:layout_width="match_parent"             android:layout_height="200dp"/>     </com.wkp.sticklayout_lib.widget.StickLayout> </LinearLayout>Note:ScrollView嵌套StickLayout时事件被拦截,无效果!StickLayout嵌套如ListView的条目控件时会只显示第一行,注意解决!代码示例public class MainActivity extends AppCompatActivity {     private StickLayout mSl;     private TextView mTv2;     private View mTv3;     private View mTv7;     private View mTv4;     private int currentPosition = -1;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         mSl = findViewById(R.id.sl);         mTv2 = findViewById(R.id.tv2);         mTv3 = findViewById(R.id.tv3);         mTv4 = findViewById(R.id.tv4);         mTv7 = findViewById(R.id.tv7); //        mSl.setStickView(findViewById(R.id.tv2)); //设置粘性控件 //        mSl.setStickView(findViewById(R.id.tv3)); //        mSl.canScrollToEndViewTop(true);      //设置是否开启最后控件滑动到顶部         //设置滑动改变监听(一滑动就会有回调)         mSl.setOnScrollChangeListener(new StickLayout.OnScrollChangeListener() {             @Override             public void onScrollChange(StickLayout v, View currentView, int position, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {                 //直到当前控件改变在做事情                 if (currentPosition != position) {                     Toast.makeText(v.getContext(), ((TextView) currentView).getText().toString(), Toast.LENGTH_SHORT).show();                     currentPosition = position;                 }             }         });     }     public void addView(View view) {         TextView textView = new TextView(view.getContext());         textView.setGravity(Gravity.CENTER);         textView.setPadding(10, 10, 10, 10);         textView.setText("新条目");         mSl.addView(textView, 0);     }     public void scrollTo2(View view) {         //滑动到指定子控件         mSl.scrollToView(mTv2);     }     public void scrollTo3(View view) {         mSl.scrollToView(mTv3);     }     public void scrollTo4(View view) {         mSl.scrollToView(mTv4);     }     public void scrollTo7(View view) {         mSl.scrollToView(mTv7);     } }Note:还有其他API请根据需要自行参考!寄语控件支持直接代码创建,还有更多API请观看StickLayout.java内的注释说明。欢迎大家使用,感觉好用请给个Star鼓励一下,谢谢!大家如果有更好的意见或建议以及好的灵感,请邮箱作者,谢谢!QQ邮箱:[email protected]邮箱:[email protected]邮箱:[email protected]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值