Android的子视图高度不匹配父的ListView项

Android的子视图高度不匹配父的ListView项

本文介绍了Android的子视图高度不匹配父的ListView项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如前所述,我的列表项是的FrameLayout ,其内部两种意见。

Col​​orView 是一个自定义视图,我为全视图显示的颜色。

的FrameLayout 的身高是WRAP_CONTENT)

这似乎在我的ICS的设备正常工作,但不能在我的Andr​​oid 2.2的模拟器和Android 1.6的G1工作。

 < XML版本=1.0编码=UTF-8&GT?;
<的FrameLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT>

    < org.mariotaku.twidere.view.ColorView
        机器人:ID =@ + ID / status_background
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:layout_gravity =中心
        机器人:背景=@可绘制/ ic_label_user/>

    < RelativeLayout的
        机器人:ID =@ + ID / status_content
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:paddingBottom会=6DP
        机器人:paddingRight =6DP
        机器人:paddingTop =6DP>

        < org.mariotaku.twidere.view.RoundCorneredImageView
            机器人:ID =@ + ID / profile_image
            机器人:layout_width =@扪/ profile_image_size
            机器人:layout_height =@扪/ profile_image_size
            机器人:layout_marginLeft =6DP
            机器人:scaleType =fitCenter/>

        <的TextView
            机器人:ID =@ + ID /名称
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignParentTop =真
            机器人:layout_alignWithParentIfMissing =真
            机器人:layout_marginLeft =6DP
            机器人:layout_toLeftOf =@ + ID /时间
            机器人:layout_toRightOf =@ + ID / profile_image
            机器人:单线=真
            机器人:textAppearance =机器人:ATTR / textAppearanceSmall
            机器人:文字颜色=机器人:ATTR / textColorPrimary
            机器人:TEXTSTYLE =黑体/>

        <的TextView
            机器人:ID =@ + ID /文
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignLeft =@ + ID /名称
            机器人:layout_alignParentRight =真
            机器人:layout_alignWithParentIfMissing =真
            机器人:layout_below =@ + ID /名称
            机器人:textAppearance =机器人:ATTR / textAppearanceSmall
            机器人:文字颜色=机器人:ATTR / textColorSecondary/>

        <的TextView
            机器人:ID =@ + ID /时间
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignBaseline =@ + ID /名称
            机器人:layout_alignParentRight =真
            机器人:layout_alignWithParentIfMissing =真
            机器人:drawablePadding =3DP
            机器人:重力=center_vertical |右
            机器人:文字颜色=机器人:ATTR / textColorSecondary/>

        < ImageView的
            机器人:ID =@ + ID / image_ preVIEW
            机器人:layout_width =@扪/ preview_image_size
            机器人:layout_height =@扪/ preview_image_size
            机器人:layout_alignWithParentIfMissing =真
            机器人:layout_below =@ + ID /文
            机器人:layout_marginLeft =16DP
            机器人:layout_marginTop =3DP
            机器人:layout_toRightOf =@ + ID / profile_image
            机器人:背景=@可绘制/ image_ preview_background
            机器人:drawablePadding =3DP
            机器人:scaleType =fitCenter
            机器人:能见度=水涨船高/>

        <的TextView
            机器人:ID =@ + ID / reply_retweet_status
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignWithParentIfMissing =真
            机器人:layout_below =@ + ID / image_ preVIEW
            机器人:layout_toRightOf =@ + ID / profile_image
            机器人:drawablePadding =3DP
            机器人:以下属性来=6DP
            机器人:paddingTop =3DP
            机器人:文字颜色=机器人:ATTR / textColorSecondary/>
    < / RelativeLayout的>

    <的TextView
        机器人:ID =@ + ID / list_gap_text
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =48dp
        机器人:layout_gravity =中心
        机器人:重力=中心
        机器人:文本=@字符串/ tap_to_load_more
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium
        机器人:TEXTSTYLE =黑体
        机器人:能见度=水涨船高/>

< /的FrameLayout>
 

它有什么解决办法或其他方式来解决这个问题?

修改

$ C $下 Col​​orView

 包org.mariotaku.twidere.view;

进口android.content.Context;
进口android.graphics.Canvas;
进口android.graphics.Color;
进口android.util.AttributeSet;
进口android.view.View;

公共类ColorView扩展视图{

    私人诠释mColor = Color.TRANSPARENT;

    公共ColorView(上下文的背景下){
        这(背景下,NULL);
    }

    公共ColorView(上下文的背景下,ATTRS的AttributeSet){
        这(背景下,ATTRS,0);
    }

    公共ColorView(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
        超(背景下,ATTRS,defStyle);
    }

    公共无效setColor(INT颜色){
        mColor =颜色;
        无效();
    }

    @覆盖
    保护无效的OnDraw(帆布油画){
        super.onDraw(画布);
        canvas.drawColor(mColor);
    }

}
 

解决方案

好难过,没有人知道如何处理这个问题。

但最后我用了一个变通方法来解决这个问题。

新的布局XML:

 < XML版本=1.0编码=UTF-8&GT?;
< org.mariotaku.twidere.view.ColorLabelRelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:填充=6DP>

    < org.mariotaku.twidere.view.RoundCorneredImageView
        机器人:ID =@ + ID / profile_image
        机器人:layout_width =@扪/ profile_image_size
        机器人:layout_height =@扪/ profile_image_size
        机器人:scaleType =fitCenter/>

    <的TextView
        机器人:ID =@ + ID /名称
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentTop =真
        机器人:layout_alignWithParentIfMissing =真
        机器人:layout_marginLeft =3DP
        机器人:layout_toLeftOf =@ + ID /时间
        机器人:layout_toRightOf =@ + ID / profile_image
        机器人:单线=真
        机器人:textAppearance =机器人:ATTR / textAppearanceSmall
        机器人:文字颜色=机器人:ATTR / textColorPrimary
        机器人:TEXTSTYLE =黑体/>

    <的TextView
        机器人:ID =@ + ID /文
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignLeft =@ + ID /名称
        机器人:layout_alignParentRight =真
        机器人:layout_alignWithParentIfMissing =真
        机器人:layout_below =@ + ID /名称
        机器人:textAppearance =机器人:ATTR / textAppearanceSmall
        机器人:文字颜色=机器人:ATTR / textColorSecondary/>

    <的TextView
        机器人:ID =@ + ID /时间
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignBaseline =@ + ID /名称
        机器人:layout_alignParentRight =真
        机器人:layout_alignWithParentIfMissing =真
        机器人:drawablePadding =3DP
        机器人:重力=center_vertical |右
        机器人:文字颜色=机器人:ATTR / textColorSecondary/>

    < ImageView的
        机器人:ID =@ + ID / image_ preVIEW
        机器人:layout_width =@扪/ preview_image_size
        机器人:layout_height =@扪/ preview_image_size
        机器人:layout_alignWithParentIfMissing =真
        机器人:layout_below =@ + ID /文
        机器人:layout_marginLeft =16DP
        机器人:layout_marginTop =3DP
        机器人:layout_toRightOf =@ + ID / profile_image
        机器人:背景=@可绘制/ image_ preview_background
        机器人:drawablePadding =3DP
        机器人:scaleType =fitCenter
        机器人:能见度=水涨船高/>

    <的TextView
        机器人:ID =@ + ID / reply_retweet_status
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignWithParentIfMissing =真
        机器人:layout_below =@ + ID / image_ preVIEW
        机器人:layout_toRightOf =@ + ID / profile_image
        机器人:drawablePadding =3DP
        机器人:以下属性来=6DP
        机器人:paddingTop =3DP
        机器人:文字颜色=机器人:ATTR / textColorSecondary/>

    <的TextView
        机器人:ID =@ + ID / list_gap_text
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =42dp
        机器人:layout_centerInParent =真
        机器人:重力=中心
        机器人:文本=@字符串/ tap_to_load_more
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium
        机器人:TEXTSTYLE =黑体
        机器人:能见度=水涨船高/>

< /org.mariotaku.twidere.view.ColorLabelRelativeLayout>
 

$ C $下 Col​​orLabelRelativeLayout

  / *
 * Twidere  -  Twitter客户端Android版
 *
 *版权所有(C)2012 Mariotaku李LT; [email protected]>
 *
 *这个程序是自由软件:您可以重新分配和/或修改
 *它在GNU通用公共许可证的条款发表
 *自由软件基金会的许可证或者3版本,或
 *(由你选择)任何更新的版本。
 *
 *这个程序是分布式的希望,这将是有益的,
 但不提供任何担保。没有的内在保证
 *适销性或特定用途的。沸腾
 * GNU通用公共许可证的更多细节。
 *
 *你应该已经收到一份GNU通用公共许可证的副本
 *随着这一计划。如果不是,请参阅< HTTP://www.gnu.org/licenses/> ;.
 * /

包org.mariotaku.twidere.view;

进口android.content.Context;
进口android.graphics.Canvas;
进口android.graphics.Color;
进口android.graphics.Paint;
进口android.graphics.Rect;
进口android.util.AttributeSet;
进口android.widget.RelativeLayout;

公共类ColorLabelRelativeLayout扩展RelativeLayout的{

    私人最终涂料mPaintLeft =新的油漆(),mPaintRight =新的油漆(),mPaintBackground =新的油漆();
    私人最终矩形mRectLeft =新的矩形(),mRectRight =新的矩形(),mRectBackground =新的矩形();
    私人最终浮动mDensity;

    公共ColorLabelRelativeLayout(上下文的背景下){
        这(背景下,NULL);
    }

    公共ColorLabelRelativeLayout(上下文的背景下,ATTRS的AttributeSet){
        这(背景下,ATTRS,0);
    }

    公共ColorLabelRelativeLayout(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
        超(背景下,ATTRS,defStyle);
        setWillNotDraw(假);
        mDensity = context.getResources()getDisplayMetrics()密度。
        mPaintLeft.setColor(Color.TRANSPARENT);
        mPaintRight.setColor(Color.TRANSPARENT);
        mPaintBackground.setColor(Color.TRANSPARENT);
    }

    公共无效drawLabel(INT左,右INT,INT背景){
        mPaintBackground.setColor(背景);
        mPaintLeft.setColor(左);
        mPaintRight.setColor(右);
        无效();
    }

    公共无效drawLeft(INT颜色){
        drawLabel(颜色,mPaintRight.getColor(),mPaintBackground.getColor());
    }

    公共无效drawRight(INT颜色){
        drawLabel(mPaintLeft.getColor(),颜色,mPaintBackground.getColor());
    }

    公共无效drawBackground(INT颜色){
        drawLabel(mPaintLeft.getColor(),mPaintRight.getColor(),颜色);
    }

    @覆盖
    公共无效的OnDraw(帆布油画){
        canvas.drawRect(mRectBackground,mPaintBackground);
        canvas.drawRect(mRectLeft,mPaintLeft);
        canvas.drawRect(mRectRight,mPaintRight);
        super.onDraw(画布);
    }

    @覆盖
    公共无效onSizeChanged(INT W,INT小时,INT oldw,诠释oldh){
        mRectBackground.set(0,0,W,H);
        mRectLeft.set(0,0,(int)的(4 * mDensity)中,h);
        mRectRight.set(瓦特 - (int)的(4 * mDensity),0,W,H);
        super.onSizeChanged(W,H,oldw,oldh);
    }
}
 

它的作品真的很适合我。

As described, my List item is a FrameLayout, there are two views inside.

ColorView is a custom view I made for show color in whole view.

(FrameLayout's height is "wrap_content")

It seems work well on my ICS device, but doesn't work on my Android 2.2 emulator and Android 1.6 G1.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <org.mariotaku.twidere.view.ColorView
        android:id="@+id/status_background"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:background="@drawable/ic_label_user"/>

    <RelativeLayout
        android:id="@+id/status_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="6dp"
        android:paddingRight="6dp"
        android:paddingTop="6dp">

        <org.mariotaku.twidere.view.RoundCorneredImageView
            android:id="@+id/profile_image"
            android:layout_width="@dimen/profile_image_size"
            android:layout_height="@dimen/profile_image_size"
            android:layout_marginLeft="6dp"
            android:scaleType="fitCenter"/>

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignWithParentIfMissing="true"
            android:layout_marginLeft="6dp"
            android:layout_toLeftOf="@+id/time"
            android:layout_toRightOf="@+id/profile_image"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="?android:attr/textColorPrimary"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/name"
            android:layout_alignParentRight="true"
            android:layout_alignWithParentIfMissing="true"
            android:layout_below="@+id/name"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="?android:attr/textColorSecondary"/>

        <TextView
            android:id="@+id/time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/name"
            android:layout_alignParentRight="true"
            android:layout_alignWithParentIfMissing="true"
            android:drawablePadding="3dp"
            android:gravity="center_vertical|right"
            android:textColor="?android:attr/textColorSecondary"/>

        <ImageView
            android:id="@+id/image_preview"
            android:layout_width="@dimen/preview_image_size"
            android:layout_height="@dimen/preview_image_size"
            android:layout_alignWithParentIfMissing="true"
            android:layout_below="@+id/text"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="3dp"
            android:layout_toRightOf="@+id/profile_image"
            android:background="@drawable/image_preview_background"
            android:drawablePadding="3dp"
            android:scaleType="fitCenter"
            android:visibility="gone"/>

        <TextView
            android:id="@+id/reply_retweet_status"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignWithParentIfMissing="true"
            android:layout_below="@+id/image_preview"
            android:layout_toRightOf="@+id/profile_image"
            android:drawablePadding="3dp"
            android:paddingLeft="6dp"
            android:paddingTop="3dp"
            android:textColor="?android:attr/textColorSecondary"/>
    </RelativeLayout>

    <TextView
        android:id="@+id/list_gap_text"
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="@string/tap_to_load_more"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="bold"
        android:visibility="gone"/>

</FrameLayout>

does it have any workaround or other way to solve this?

EDIT

code for ColorView

package org.mariotaku.twidere.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;

public class ColorView extends View {

    private int mColor = Color.TRANSPARENT;

    public ColorView(Context context) {
        this(context, null);
    }

    public ColorView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ColorView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setColor(int color) {
        mColor = color;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(mColor);
    }

}
解决方案

so sad, nobody knows how to deal with this.

but finally I used a workaround to solve this problem.

new layout XML:

<?xml version="1.0" encoding="utf-8"?>
<org.mariotaku.twidere.view.ColorLabelRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="6dp">

    <org.mariotaku.twidere.view.RoundCorneredImageView
        android:id="@+id/profile_image"
        android:layout_width="@dimen/profile_image_size"
        android:layout_height="@dimen/profile_image_size"
        android:scaleType="fitCenter"/>

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignWithParentIfMissing="true"
        android:layout_marginLeft="3dp"
        android:layout_toLeftOf="@+id/time"
        android:layout_toRightOf="@+id/profile_image"
        android:singleLine="true"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="?android:attr/textColorPrimary"
        android:textStyle="bold"/>

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/name"
        android:layout_alignParentRight="true"
        android:layout_alignWithParentIfMissing="true"
        android:layout_below="@+id/name"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="?android:attr/textColorSecondary"/>

    <TextView
        android:id="@+id/time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/name"
        android:layout_alignParentRight="true"
        android:layout_alignWithParentIfMissing="true"
        android:drawablePadding="3dp"
        android:gravity="center_vertical|right"
        android:textColor="?android:attr/textColorSecondary"/>

    <ImageView
        android:id="@+id/image_preview"
        android:layout_width="@dimen/preview_image_size"
        android:layout_height="@dimen/preview_image_size"
        android:layout_alignWithParentIfMissing="true"
        android:layout_below="@+id/text"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="3dp"
        android:layout_toRightOf="@+id/profile_image"
        android:background="@drawable/image_preview_background"
        android:drawablePadding="3dp"
        android:scaleType="fitCenter"
        android:visibility="gone"/>

    <TextView
        android:id="@+id/reply_retweet_status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignWithParentIfMissing="true"
        android:layout_below="@+id/image_preview"
        android:layout_toRightOf="@+id/profile_image"
        android:drawablePadding="3dp"
        android:paddingLeft="6dp"
        android:paddingTop="3dp"
        android:textColor="?android:attr/textColorSecondary"/>

    <TextView
        android:id="@+id/list_gap_text"
        android:layout_width="wrap_content"
        android:layout_height="42dp"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:text="@string/tap_to_load_more"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="bold"
        android:visibility="gone"/>

</org.mariotaku.twidere.view.ColorLabelRelativeLayout>

code for ColorLabelRelativeLayout:

/*
 *              Twidere - Twitter client for Android
 *
 * Copyright (C) 2012 Mariotaku Lee <[email protected]>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.mariotaku.twidere.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

public class ColorLabelRelativeLayout extends RelativeLayout {

    private final Paint mPaintLeft = new Paint(), mPaintRight = new Paint(), mPaintBackground = new Paint();
    private final Rect mRectLeft = new Rect(), mRectRight = new Rect(), mRectBackground = new Rect();
    private final float mDensity;

    public ColorLabelRelativeLayout(Context context) {
        this(context, null);
    }

    public ColorLabelRelativeLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ColorLabelRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setWillNotDraw(false);
        mDensity = context.getResources().getDisplayMetrics().density;
        mPaintLeft.setColor(Color.TRANSPARENT);
        mPaintRight.setColor(Color.TRANSPARENT);
        mPaintBackground.setColor(Color.TRANSPARENT);
    }

    public void drawLabel(int left, int right, int background) {
        mPaintBackground.setColor(background);
        mPaintLeft.setColor(left);
        mPaintRight.setColor(right);
        invalidate();
    }

    public void drawLeft(int color) {
        drawLabel(color, mPaintRight.getColor(), mPaintBackground.getColor());
    }

    public void drawRight(int color) {
        drawLabel(mPaintLeft.getColor(), color, mPaintBackground.getColor());
    }

    public void drawBackground(int color) {
        drawLabel(mPaintLeft.getColor(), mPaintRight.getColor(), color);
    }

    @Override
    public void onDraw(Canvas canvas) {
        canvas.drawRect(mRectBackground, mPaintBackground);
        canvas.drawRect(mRectLeft, mPaintLeft);
        canvas.drawRect(mRectRight, mPaintRight);
        super.onDraw(canvas);
    }

    @Override
    public void onSizeChanged(int w, int h, int oldw, int oldh) {
        mRectBackground.set(0, 0, w, h);
        mRectLeft.set(0, 0, (int)(4 * mDensity), h);
        mRectRight.set(w - (int)(4 * mDensity), 0, w, h);
        super.onSizeChanged(w, h, oldw, oldh);
    }
}

it works really well for me.

这篇关于Android的子视图高度不匹配父的ListView项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 03:19