经常碰到需要把一个控件放在手机底部的情况,以前都是在LinearLayout尝试使用gravity="bottom" ,但是,没有效果,后来在网上查到了方法,如下

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical"
  5. >
  6.  
  7. <TextView
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:layout_marginStart="12dp"
  11. android:layout_marginEnd="12dp"
  12. android:layout_marginTop="12dp"
  13. android:textSize="15sp"
  14. android:textColor="#323232"
  15. android:text="@string/master_clear_final_desc" />
    <!--这个是关键,就是占满次下屏幕的所有位置-->
         <LinearLayout
  16. android:id="@+id/content"
  17. android:layout_width="fill_parent"
  18. android:layout_height="0dp"
  19. android:layout_weight="1"
  20. android:orientation="vertical">
  21.  
  22. </LinearLayout>
  23. <Button android:id="@+id/execute_master_clear"
  24. style="@style/SecurityPreferenceButtonFunuiConfirm"
  25. android:text="@string/master_clear_final_button_text" />
  26. </LinearLayout>

至于android:layout_height="0dp" android:layout_weight="1"  它的意思就是,尽可能的占据父容器空闲的位置,而且,它不会把其他控件覆盖或者挤出去。

我们看如下的代码

  1. <LinearLayout
  2. android:layout_width="0dp"
  3. android:layout_height="35dp"
  4. android:layout_weight="1"
  5. android:background="@drawable/search_bg_funui"
  6. android:orientation="horizontal"
  7. android:layout_gravity="center_vertical"
  8. >
  9.  
  10. <EditText
  11. android:id="@+id/search_actionbar_query_text"
  12. android:layout_width="0dp"
  13. android:layout_height="match_parent"
  14. android:layout_weight="1"
  15. android:background="@null"
  16. android:gravity="center_vertical"
  17. android:hint="@string/search_hint"
  18. android:imeOptions="actionSearch|flagNoExtractUi"
  19. android:inputType="text|textNoSuggestions"
  20. android:nextFocusDown="@+id/search_overlay_suggestion_list"
  21. android:paddingLeft="@dimen/search_main_text_padding"
  22. android:paddingRight="@dimen/search_main_text_padding"
  23. android:singleLine="true"
  24. android:textAlignment="viewStart"
  25. android:textColor="@color/search_query_text"
  26. android:textColorHint="@color/search_query_hint_text"
  27. android:textCursorDrawable="@drawable/display_input_cursor"
  28. android:textSize="16sp" />
  29.  
  30. <ImageView
  31. android:id="@+id/search_actionbar_ending_button"
  32. android:layout_width="@dimen/search_ending_button_width"
  33. android:layout_height="match_parent"
  34. android:background="?attr/selectableItemBackgroundBorderless"
  35. android:scaleType="center" />
  36. </LinearLayout>

在上述代码中, EditText尽量占有LinearLayout的位置,这样就会把ImageView挤到最边缘位置。但是,它的原则是,永远给同事控件保留设定的属性要求

我们经常会实现这样的功能,就是在listview底部跟一个按钮。而且,这个按钮一定要一直显示在底部。这时候,我们就可以通过设定listview的属性为

  1. android:layout_width="0dp"
  2. android:layout_height="match_parent"
  3. android:layout_weight="1"

就行了,哪怕这时候listview没有item,listview也会尽可能充满这个屏幕,如果listview的item超过整个屏幕,它也会自动添加滚动条,而不会把下方的按钮挤出或覆盖

android-LinearLayout 控件占满父容器位置实现的更多相关文章

  1. javascript客户端遍历控件与获取父容器对象

    javascript客户端遍历控件与获取父容器对象示例代码 1,遍历也面中所有的控件function findControlAll()    {        var inputs=document. ...

  2. Android单个控件占父控件宽度一半且水平居中

    前些天,在工作中遇到了一个需求:一个“加载上一页”的按钮宽度为父控件宽度一半,且水平居中于父控件中. 在此给出两种思路: 1.直接在Activity代码中获取到当前父控件的宽度,并将此按钮宽度值设置成 ...

  3. 【Unity】UGUI控件大小适配父容器

    需求:需要把UGUI控件的尺寸调整到指定大小,如匹配至设计的分辨率.或者说想制定覆盖全屏的背景图片. 做法:将这个UGUI控件的RectTransform组件里的Anchor Presets设为预设的 ...

  4. 【WPF】Viewbox标签——控件大小适应父容器

    需求:图片拉伸至填满Image控件. 使用标签进行嵌套. <Grid> <Viewbox> <Image Name="myImage" /> & ...

  5. android基本控件学习-----TextView

    一.TextView的讲解 <实例一> <?xml version="1.0" encoding="utf-8"?> <Linea ...

  6. Android:控件布局(相对布局)RelativeLayout

    RelativeLayout是相对布局控件:以控件之间相对位置或相对父容器位置进行排列. 相对布局常用属性: 子类控件相对子类控件:值是另外一个控件的id android:layout_above-- ...

  7. Android:控件布局(相对布局)RelativeLayout(转)

    相对布局常用属性: 子类控件相对子类控件:值是另外一个控件的id android:layout_above----------位于给定DI控件之上android:layout_below ------ ...

  8. c# winform控件dock属性停造位置、摆放顺序详解

    dock : [英文释义- 码头.依靠][winform释义- 获取或设置当前控件依靠到父容器的哪一个边缘.] 用途:多数控件都有这个属性,主要用来设置控件的布局. 但对于不太了解这个属性的朋友来说有 ...

  9. Android:控件布局(线性布局)LinearLayout

    LinearLayout是线性布局控件:要么横向排布,要么竖向排布 决定性属性:必须有的! android:orientation:vertical (垂直方向) .horizontal(水平方向) ...

随机推荐

  1. P4287 [SHOI2011]双倍回文(回文树)

    题目描述 记字符串 w 的倒置为 w^R^ .例如 (abcd)^R^=dcba , (abba)^R^=abba . 对字符串x,如果 x 满足 x^R^=x ,则称之为回文:例如abba是一个回文 ...

  2. 现在有一个函数A和函数B,请你实现B继承A

    现在有一个函数A和函数B,请你实现B继承A // 方式1 function B(){} function A(){} B.prototype = new A(); // 方式2 function A( ...

  3. POJ——T 2449 Remmarguts' Date

    http://poj.org/problem?id=2449 Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 30754   ...

  4. 零基础学python-3.3 标识符

    1.标识符的组成 1)有数字.下划线.英文字母组成 2)第一个字符仅仅能是字母或者下划线 3)大写和小写敏感 标识符通常是变量名称.方法名.类名等 2.keyword python里面有一系列的关键字 ...

  5. POJ 1887 Testingthe CATCHER (LIS:最长下降子序列)

    POJ 1887Testingthe CATCHER (LIS:最长下降子序列) http://poj.org/problem?id=3903 题意: 给你一个长度为n (n<=200000) ...

  6. spring配置 quartz-config.xml

    <!-- 配置调度程序quartz ,其中配置JobDetail有两种方式--> <!-- 使用MethodInvokingJobDetailFactoryBean,任务类可以不实现 ...

  7. BZOJ 3544 treap (set)

    我只是想找个treap的练习题-- 每回找到lower_bound 就好啦 //By SiriusRen #include <cstdio> #include <cstring> ...

  8. jdbc参数传递

    1.jdbc请求设置 将查询结果第一列coupon_id,存放在couponId中; 将查询结果第二列code,存放在coupCode中 2.参数解释: couponId_#:表示查询结果中coupo ...

  9. Python正则表达式初识(九)

    继续分享Python正则表达式的基础知识,今天给大家分享的特殊字符是[\u4E00-\u9FA5],这个特殊字符最好能够记下来,如果记不得的话通过百度也是可以一下子查到的. 该特殊字符是固定的写法,其 ...

  10. vue.js中compted与model的区别

    在p便签内写的{{reversemessage}}方法,若js里对应的函数为computed则不需要加上括号 若js里对应的函数为model则应该将{{reversemessage}}改为{{reve ...