Java 自定义视图未显示:无高度,但仍显示宽度
在我目前的项目中,我陷入了死胡同。我的自定义视图(仅为显示特定颜色而创建)不会显示在模拟器的视图中(尽管它似乎在Eclipse的编辑器中正确显示),除非它有一个最小高度,但它只能得到这个精确的高度。我希望它与父项匹配 问题可能是它位于列表视图的项目布局中 这是我的自定义视图的代码:Java 自定义视图未显示:无高度,但仍显示宽度,java,android,android-layout,android-custom-view,custom-view,Java,Android,Android Layout,Android Custom View,Custom View,在我目前的项目中,我陷入了死胡同。我的自定义视图(仅为显示特定颜色而创建)不会显示在模拟器的视图中(尽管它似乎在Eclipse的编辑器中正确显示),除非它有一个最小高度,但它只能得到这个精确的高度。我希望它与父项匹配 问题可能是它位于列表视图的项目布局中 这是我的自定义视图的代码: import android.content.Context; import android.content.res.Resources; import android.content.res.TypedArray;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class ColorView extends View {
private int mColor;
private Context mContext;
private int width;
private int height;
public ColorView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ColorView, 0, 0);
try {
mColor = a.getColor(R.styleable.ColorView_color, 0xFFFFFF);
} finally {
a.recycle();
}
}
public void setColor(int hexColor) {
mColor = hexColor;
invalidate();
}
public void setColorFromResource(int resID) {
mColor = mContext.getResources().getColor(resID);
invalidate();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
Log.d("DEBUG", Integer.toString(w) + " " + Integer.toString(h));
width = w;
height = h;
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(mColor);
Log.d("DEBUG", "drawn");
}
}
以及我的布局文件的简化版本:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:raabeapp="http://schemas.android.com/apk/res/com.example.myapp"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/row_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<com.example.myapp.ColorView
android:id="@+id/status_view"
android:layout_width="20dip"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dip"
android:minHeight="50dp"
raabeapp:color="#FF0000" />
<TextView
android:id="@+id/hour_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/status_view"
android:text="1"
android:textSize="@dimen/hour_textsize"
android:width="70dp" />
<TextView
android:id="@+id/text_hourtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/hour_view"
android:text="@string/hour_text"
android:textSize="@dimen/hourtext_textsize" />
使用android:layout\u height=“match\u parent”
为您的RelativeLayout
好的,我找到了一个解决方法:当我使用
线性布局
而不是相对布局
时,我得到了想要的结果。问题似乎在于
相对性yout
。如果有人知道这是为什么,或者我如何仍然可以使用RelativeLayout
,我会非常感兴趣。这个答案并不能提供一个完整的解决方案,但如果有人遇到我的问题,它可能会提供一个丑陋的解决办法。这并不能提供问题的答案。若要评论或要求作者澄清,请在其帖子下方留下评论。@RobWatts它确实回答了问题。它可能是对的,也可能是不对的(不打算测试),但在Android世界中,这是一件值得尝试的事情。对于我最近的回答,我只是看到了你的评论。好主意,但它不起作用。问题不在于行布局不适合行—我得到的组件有时比视图在最大大小时得到的组件高—而是视图根本没有高度,尽管它应该是这样的。