美文网首页
Android View 属性中的坑

Android View 属性中的坑

作者: 梦凝天 | 来源:发表于2016-05-02 11:38 被阅读336次

    android:layout_height android:layout_width android:bakground属性问题:

    按道理来说控件背景的高宽应该是跟控件一般大小,但是事实不是如此,如果android:layout_height值为wrap_content 时如果控件度量的大小小于背景则 控件的大小按背景来算 这时候不知为何会出现子控件位置设置出现问题 建议layout_height的高度设置数值 当然纯色的colorbackground 不存在这个问题

    match_parent wrap_content问题

    假设RelativeLayout有2个child,一个child有高度, 一个child的高度设置为match_parent,同时relativeLayout的最终父类是scrollView。
    如果viewGroup 高度设置为wrap_content, 这时设置为match_parent 的child的高度为0。 初步查看了RelativeLayout的源码里面传递给child的高度为0(特指child高度设置为match_parent,parent 高度为wrap_content ),但是如果child里面有设置居中的属性,则会设置child居中

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="20dp"
                android:text="nihao "/>
    
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@android:color/holo_red_dark"
                />
        </RelativeLayout>
    
    </ScrollView>
    
    QQ截图20160502165137.png

    2.

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="20dp"
                android:text="nihao "/>
    
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@android:color/holo_red_dark"
                />
        </RelativeLayout>
    
    </RelativeLayout>
    
    QQ截图20160502165150.png

    1跟2的显示效果不一样,1符合上述情况 2则明显充满整个屏幕

    相关文章

      网友评论

          本文标题:Android View 属性中的坑

          本文链接:https://www.haomeiwen.com/subject/villrttx.html