Ok, I figured out a workaround:
When I use a LinearLayout
instead of a RelativeLayout
I get the result I want.
It seems that the problem lies in the RelativeLayout
. If someone knows why this is, or how I could still use a RelativeLayout
I would be very interested. This answer does not provide a full solution, but in case someone runs into my problem it could provide a ugly workaround.
Custom View not showing: no height, but still width
-
19-07-2023 - |
문제
In my current project I run into an dead end. My custom view, only created to show a specific color, won't show in the view of my emulator (although it seems to show up correctly in the editor of Eclipse) except if it had a minimum height, but then it only get this exact height. I want it to match_parent
.
The problem might be that it is in the item layout of a list view.
That's the code of my Custom View:
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");
}
}
And a simplified version of my layout file:
<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" />
해결책
다른 팁
Use android:layout_height="match_parent"
for your RelativeLayout
.
제휴하지 않습니다 StackOverflow