如上所述,我的列表项是一个FrameLayout
,里面有两个 View 。
ColorView
是我为在整个 View 中显示颜色而制作的自定义 View 。
(FrameLayout
的高度是 "wrap_content")
它似乎在我的 ICS 设备上运行良好,但在我的 Android 2.2 模拟器和 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>
是否有任何解决方法或其他方法来解决此问题?
编辑
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);
}
}
最佳答案
我们刚刚不得不处理类似的问题。我们注意到,在作为 ListView 项目一部分的 View 上使用 match_height 不会按预期工作。以下代码应该有效:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/color_bar" >
<!-- Put the content views of your item here. You do not have to use frame layout, it can be any kind of view. -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="@string/app_name" />
</FrameLayout>
<View
android:id="@id/color_bar"
android:layout_width="10dp"
android:layout_height="match_parent"
android:layout_alignBottom="@id/content"
android:layout_alignParentRight="true"
android:layout_alignTop="@id/content"
android:background="@android:color/darker_gray" />
</RelativeLayout>
技巧基本上是将彩色 View 与内容 View 的顶部和底部对齐。无论内容的高度是多少,它都会被彩色 View 采用。
旁注,如 Android's documentation 中所述,您不应使用 FrameLayout 来包含多个子项。
编辑
我的同事让我注意到,如果我们使用 LinearLayout 作为 ListView 项的根元素,match_parent 会按预期工作:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="8dp"
android:text="@string/app_name" />
<View
android:id="@+id/color_bar"
android:layout_width="10dp"
android:layout_height="match_parent"
android:background="@android:color/darker_gray" />
</LinearLayout>
我在装有 Android 2.2 的设备上对此进行了测试,它运行正常。
关于Android subview 高度与 ListView 项目中的父项不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12208037/