android 代码布局设置wrap_content,Android布局文件中wrap_content和0dp的区别

Layout下的布局设置Widget宽高的填充形式:

(1)match_parent:指占满父容器此时要控件的宽或高等于父容器的宽或高。

(2)wrap_content和的用法:指控件的高或宽随内容的长度决定。

(3)设置固定值,可以是30dp,也可以是120dp,想要设置为0dp,必须有weight属性,且值不为0才可以。

不同布局效果

(1)第一种情况:

android:background="@color/white"

android:layout_width="match_parent"

android:layout_height="100dp"

android:orientation="horizontal">

android:layout_width="wrap_content"

android:layout_height="wrap_content"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"/>

效果图:

9836694dace1

case1.png

(2)第二种情况:

android:background="@color/white"

android:layout_width="match_parent"

android:layout_height="100dp"

android:orientation="horizontal">

android:layout_width="wrap_content"

android:layout_height="wrap_content"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"/>

效果图:

9836694dace1

case2.png

(3)第三种情况:

android:background="@color/white"

android:layout_width="match_parent"

android:layout_height="100dp"

android:orientation="horizontal">

android:layout_width="wrap_content"

android:layout_height="match_parent"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"/>

效果图:

9836694dace1

case3.png

(4)第四种情况:

android:background="@color/white"

android:layout_width="match_parent"

android:layout_height="100dp"

android:orientation="horizontal">

android:layout_width="wrap_content"

android:layout_height="wrap_content"/>

android:layout_width="match_parent"

android:layout_height="match_parent"/>

效果图:

9836694dace1

case4.png

(5)第五种情况:

android:background="@color/white"

android:layout_width="match_parent"

android:layout_height="100dp"

android:orientation="horizontal">

android:text="Btn1"

android:layout_width="match_parent"

android:layout_height="wrap_content" />

android:text="Btn2"

android:layout_width="match_parent"

android:layout_height="wrap_content"/>

效果图:

9836694dace1

case5.png

(6)第六种情况:

android:background="@color/white"

android:layout_width="match_parent"

android:layout_height="100dp"

android:orientation="horizontal">

android:text="Btn1"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="wrap_content" />

android:text="Btn2"

android:layout_width="match_parent"

android:layout_height="wrap_content"/>

效果图:

9836694dace1

case6.png

设置比重时需要改为0dp的问题

而当我们使用到比重的时候,会在代码中有提示:让我们将layout_width的值设置为0dp

9836694dace1

casedemo.png

设置之后:

android:background="@color/white"

android:layout_width="match_parent"

android:layout_height="100dp"

android:orientation="horizontal">

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="wrap_content" />

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="wrap_content"/>

效果图:均分铺满

9836694dace1

case7.png

而如果我们的布局如下:将layout_width的值设置为wrap_content

android:background="@color/white"

android:layout_width="match_parent"

android:layout_height="100dp"

android:orientation="horizontal">

android:layout_width="wrap_content"

android:layout_weight="1"

android:layout_height="wrap_content" />

android:layout_width="wrap_content"

android:layout_weight="1"

android:layout_height="wrap_content"/>

那么效果图:也是均分铺满

9836694dace1

case8.png

那么问题来了,到底为什么代码提示我们需要将layout_width的值设置为0dp呢?

答案:事实是这样的,系统会先根据width和height属性首次对控件进行排版,然后在查看是否分配了权重,在按照权重二次分配控件。这样就执行了两次。如果将width(或height)为0dp就执行一次就可以了,提高运行性能。避免重复排版。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 开发 (实验五) 实验题目:Android 组件布局试验 指导老师: 班 级:计算机科学与技术系班 姓 名: 一、实验目的 1、掌握 Android 组件布局的使用方法 2、学会组件布局的重要属性与应用 3、能够根据需求,通过布局构建各类实际的页面。 二、实验内容 组 件 布 局 有 : LinearLayout 、 TableLayout 、 FrameLayout 、 RelativeLayout 三、实验步骤 1、用表格布局完成登录界面 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FFFFCC" android:stretchColumns="0,3" > <ImageView android:layout_width="fill_parent" android:layout_height="5dp"/> <TableRow> <ImageView android:layout_width="wrap_content" android:layout_height="100dp" android:src="@drawable/img" android:gravity="center" android:layout_span="4"/> </TableRow> <TableRow > <TextView/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="right" android:text="@string/username" android:textStyle="bold" android:textColor="#000000"/> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="left" android:minWidth="200px" android:id="@+id/ETname"/> </TableRow> <TableRow > <TextView/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="right" android:text="@string/password" android:textStyle="bold" android:textColor="#000000"/> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="left" android:minWidth="200px" android:inputType="textPassword" android:id="@+id/ETpwd"/> </TableRow> <TableRow> <TextView /> <Button android:id="@+id/Btok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="right" android:text="@string/button" android:textStyle="bold"/> <Button android:id="@+id/Btcancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="left" android:text="@string/cancel" android:textStyle="bold"/> </TableRow> </TableLayout> 2、使用线性布局完成系统主界面 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/an
ToggleExpandLayout是一个可折叠和展开子view的开关布局控件。它可以将它的子view以阶梯式的展开。项目地址:https://github.com/fenjuly/ToggleExpandLayout 效果图:如何使用<com.fenjuly.mylibrary.ToggleExpandLayout             android:id="@ id/toogleLayout"             android:layout_width="wrap_content"             android:layout_height="80dp"             >             <TextView                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:text="view 1"/>             <TextView                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:text="view 2"/>             <TextView                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:text="view"/>                      </com.fenjuly.mylibrary.ToggleExpandLayout>注意,由于ToggleExpandLayout的本质是个FrameLayout,所以必须将其高度设置为大于所有子view展开状态的高度,不能设为wrap_content。为了解决这个问题,你可以将ToggleExpandLayout的外面在加个DropDownLayout:<com.fenjuly.mylibrary.DropDownLayout         android:layout_width="match_parent"         android:layout_height="match_parent"         >         <com.fenjuly.mylibrary.ToggleExpandLayout             android:id="@ id/toogleLayout"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             >             <TextView                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:text="view 1"/>              <TextView                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:text="view 2"/>               <TextView                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:text="view"/>                              </com.fenjuly.mylibrary.ToggleExpandLayout> </com.fenjuly.mylibrary.DropDownL
粘性控件,其任意一个子控件都可滑动停留,无论是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、付费专栏及课程。

余额充值