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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginEnd="12dp"
android:layout_marginTop="12dp"
android:textSize="15sp"
android:textColor="#323232"
android:text="@string/master_clear_final_desc" />
<!--这个是关键,就是占满次下屏幕的所有位置-->
     <LinearLayout
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"> </LinearLayout>
<Button android:id="@+id/execute_master_clear"
style="@style/SecurityPreferenceButtonFunuiConfirm"
android:text="@string/master_clear_final_button_text" />
</LinearLayout>

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

我们看如下的代码

    <LinearLayout
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:background="@drawable/search_bg_funui"
android:orientation="horizontal"
android:layout_gravity="center_vertical"
> <EditText
android:id="@+id/search_actionbar_query_text"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@null"
android:gravity="center_vertical"
android:hint="@string/search_hint"
android:imeOptions="actionSearch|flagNoExtractUi"
android:inputType="text|textNoSuggestions"
android:nextFocusDown="@+id/search_overlay_suggestion_list"
android:paddingLeft="@dimen/search_main_text_padding"
android:paddingRight="@dimen/search_main_text_padding"
android:singleLine="true"
android:textAlignment="viewStart"
android:textColor="@color/search_query_text"
android:textColorHint="@color/search_query_hint_text"
android:textCursorDrawable="@drawable/display_input_cursor"
android:textSize="16sp" /> <ImageView
android:id="@+id/search_actionbar_ending_button"
android:layout_width="@dimen/search_ending_button_width"
android:layout_height="match_parent"
android:background="?attr/selectableItemBackgroundBorderless"
android:scaleType="center" />
</LinearLayout>

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

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

        android:layout_width="0dp"
android:layout_height="match_parent"
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. C# http服务器

    Http 服务器搭建 1.新建一个C#控制台工程 2.复制以下代码 using System; using System.Collections.Generic; using System.Linq; ...

  2. Java基础学习总结(26)——JNDI入门简介

    JNDI是 Java 命名与目录接口(Java Naming and Directory Interface),在J2EE规范中是重要的规范之一,不少专家认为,没有透彻理解JNDI的意义和作用,就没有 ...

  3. Android Studio获取开发版SHA1值和发布版SHA1值,详细过程

    转自原文 Android Studio获取开发版SHA1值和发布版SHA1值的史上最详细方法 前言: 今天我想把百度地图的定位集成到项目中来,想写个小小的案例,实现一下,但在集成百度地图时首先要申请秘 ...

  4. JSTL标准标签库具体解释

    JSTL标签库的使用是为类弥补html表的不足.规范自己定义标签的使用而诞生的. 在告别modle1模式开发应用程序后.人们開始注重软件的分层设计,不希望在jsp页面中出现java逻辑代码,同一时候也 ...

  5. 直播聊天室,点亮效果,jquery实现

    1.css #like_area img{ width: 30px; height: 30px; position: absolute; bottom: 100px; left: 60%; margi ...

  6. Weka中数据挖掘与机器学习系列之Weka Package Manager安装所需WEKA的附加算法包出错问题解决方案总结(八)

    不多说,直接上干货! Weka中数据挖掘与机器学习系列之Weka系统安装(四) Weka中数据挖掘与机器学习系列之Weka3.7和3.9不同版本共存(七) 情况1 对于在Weka里,通过Weka  P ...

  7. 配置CiscoWorks 2000 ANI同步

    配置CiscoWorks 2000 ANI同步       在CiscoWorks 2000的LAN ManagementSolution(LMS)中,Cisco包含了一种ANI的自动发现过程和Res ...

  8. SpringMVC与SpringBoot返回静态页面遇到的问题

    1.SpringMVC静态页面响应 package com.sv.controller; import org.springframework.stereotype.Controller; impor ...

  9. 学习“花书“《深度学习》中文PDF和英文PDF

    个人觉得github上的中文版翻译的不错,有700多页,深度学习入门经典书籍,前几章的数学基础介绍的相当不错. 第一部分基本就是统计学习最基础的线性代数,概率论等,第4章值得一读,讲了些数值分析里常涉 ...

  10. OpenJDK源码研究笔记(十三):Javac编译过程中的上下文容器(Context)、单例(Singleton)和延迟创建(LazyCreation)3种模式

    在阅读Javac源码的过程中,发现一个上下文对象Context. 这个对象用来确保一次编译过程中的用到的类都只有一个实例,即实现我们经常提到的"单例模式". 今天,特意对这个上下文 ...