Android 设置子控件的宽度或高度为 match_parent来填充父控件中的剩余宽度或高度的方法

先上几张效果图, 如下:

    

 

 

上述四张图要实现的布局效果是: 假如父控件中包含两个子控件, 其中一个子控件(上图中为红色button)的宽度是固定数值, 而另一个子控件(上图中为绿色button)的宽度不固定, 要想让这两个子控件的总宽度刚好等于父控件的宽度.可以将宽度不固定的那个控件的宽度设置为match_parent来实现, 但有些细节需要注意, 否则即使设置了match_parent, 也不能出现如上的效果. 注意细节如下:

  1. 上述效果可以使用RelativeLayout实现. 但注意绿色button的水平位置必须相对于红色button来设置, 而不能相对于父控件来设置. 即: 绿色button只能设置为android:layout_toLeftOf="@id/red_button" 或 android:layout_toRightOf="@id/red_button", 而不能设置为 android:layout_alignParentLeft="true" 或 android:layout_alignParentRight="true". 否则两个button将会产生重叠.
  2. 上述效果如果使用LinearLayout实现, 将有一定局限性. 只有当宽度确定的控件都位于左边(或上边), 宽度不确定的控件位于右边(或下边)时, 才能使用LinearLayout. 也就是说, 使用LinearLayout只能实现前两个图的效果, 而不能实现后两个图的效果(如果用于后两个效果, 那么左边的button将占据整个屏幕的宽度, 而右边的button将会被挤到屏幕外了).

附上布局代码:

1. 图1的布局:

(1) 使用RelativeLayout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/red_button"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:background="#ff0000"
        android:text="1" />

    <Button
        android:id="@+id/green_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/red_button"
        android:background="#00ff00"
        android:text="222222" />

</RelativeLayout>

(2) 使用LinearLayout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/red_button"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:background="#ff0000"
        android:text="1" />

    <Button
        android:id="@+id/green_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#00ff00"
        android:text="222222" />

</LinearLayout>

2. 图2的布局:

(1) 使用RelativeLayout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/red_button"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:background="#ff0000"
        android:text="1" />

    <Button
        android:id="@+id/green_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/red_button"
        android:background="#00ff00"
        android:text="222222" />

</RelativeLayout>

(2) 使用LinearLayout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/red_button"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:background="#ff0000"
        android:text="1" />

    <Button
        android:id="@+id/green_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#00ff00"
        android:text="222222" />

</LinearLayout>

3. 图3的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/red_button"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="#ff0000"
        android:text="1" />

    <Button
        android:id="@+id/green_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/red_button"
        android:background="#00ff00"
        android:text="222222" />

</RelativeLayout>

4. 图4的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/red_button"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="#ff0000"
        android:text="1" />

    <Button
        android:id="@+id/green_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/red_button"
        android:background="#00ff00"
        android:text="222222" />

</RelativeLayout>
时间: 2024-07-29 18:15:27

Android 设置子控件的宽度或高度为 match_parent来填充父控件中的剩余宽度或高度的方法的相关文章

dev设置子窗体的初始位置,grid控件表头的属性设置

当在父窗体上弹出子窗体时,一般设置子窗体的初始位置是居中, //在需要展示子窗体的父窗体上写这段,注意必须设置在show方法之前Form2 f2 = new Form2(); f2.MdiParent = this; f2.StartPosition = FormStartPosition.CenterScreen; //CenterScreen相对于屏幕居中,CenterParent是相对于父窗体居中 f2.Show(); DEV中Grid禁用表头的右键菜单,设置属性如下图: DEV中Grid

android如果重写onDraw实现一个类似TextView可以显示表情和链接的控件(一)

先看效果图: 写一个超连接支持的对象: /**作为超连接显示的对象*/ public class LinkInfo implements Comparable<LinkInfo>{ private String content; private String type; private String id; private boolean bIsFace = false; private boolean bSelected = false; public static final String

Android两个控件叠在一起,如何让被挡住的控件显示出来

Android两个控件叠在一起,如何让被挡住的控件显示出来 问题 : 两个控件叠在一起,如何让被挡住的控件显示出来? 比如A,B两个控件,A被B挡住,目前A要显示出来,B不能被隐藏,A的高度只有那么一点,显示出来的时候,B不能隐藏. 其实很简单 A.bringToFront即可.

如果希望点击父控件子控件也响应的话, 可以给子控件加如下属性: ?android:duplicateParentState="true"

如果希望点击父控件子控件也响应的话, 可以给子控件加如下属性: android:duplicateParentState="true" 来自为知笔记(Wiz)

Android开发之解决父控件拦截子控件事件问题

以ViewPager为例: 1 public class TopNewsViewPager extends ViewPager { 2 public TopNewsViewPager(Context context) { 3 super(context); 4 } 5 6 public TopNewsViewPager(Context context, AttributeSet attrs) { 7 super(context, attrs); 8 } 9 10 //重写这个方法,并且在方法里面

Android知识点:设置父控件事件拦截

@Override public boolean dispatchTouchEvent(MotionEvent ev) { //请求所有父控件不要拦截Touch事件 getParent().requestDisallowInterceptTouchEvent(true); return super.dispatchTouchEvent(ev); }

子控件根据父控件行宽自动换行---LineWrapLayout实现

一些带搜索功能的app,在搜索栏下面一般会提供一些关键字供用户选择. 也可以根据用户输入的文字,在下一次使用的时候该文字出现在常用关键字里面,只要轻轻一点就可以搜索了,无需再次输入. 关键字可以动态添加,这就要考虑换行的问题了 废话不多说,先上效果图: 先定义2个自定义属性 <declare-styleable name="linewarplayout"> <attr name="magin" format="integer"

android应用开发--------------看RadioGroup源码,写类似单选选项卡的集成控件(如底部导航,tab等等)

博客为 有时个哥 原创,如需转载请标明出处:http://blog.csdn.net/ls703/article/details/46694967 上面就是需求设计,4个类似的布局控件,每次只能选择一个,然后得到上面对应的钱数.(上面只是效果图,实际数据是从服务器获取,然后付到控件上) 看到这种,我们就回想到,几种实现方法. 1.把这个整体写一个布局,在xml布局中,复制粘贴,代码,凑够4个.非常不建议这样,因为4个的布局样式是一样的,只是数据可能不相同,所以我们应该写一个组合控件然后重复利用.

WPF随笔之 控件根据设定的显示行数列数填充控件并自适应窗体大小(多绑定MVVM方式实现)

(效果图,如见最下面) 需求:根据设置的行数列数,控制展示控件个数,并且填充的控件们大小刚刚好自适应填充满固定的区域,并且调整窗体大小的时候控件动态自适应窗体大小,即自适应大小并不显示滚动条(比如,设置了1行1列,则第一页显示一个控件,如设置了2行2列,则第一页显示第一行2个控件,第二行2个控件). 解决方案,我总结有3 1.在cs代码里面动态生成Grid控件,根据设定的行列动态生成行列,将控件自适应宽高填充进去 2.固定Grid.使用WrapPanel排序,当Grid实际宽高发生改变时,动态计