내 사용자 정의보기, ColorStrip
을 내 응용 프로그램의 두 곳에서 사용합니다 : ListView
항목과 별도 FragmentActivity
입니다. ListView 항목은 내보기를 올바르게 표시하지만 ColorStrip
이 내 FragmentActivity
에 대해 만들어진 이상한 이유로 hPx
및 wPx
변수는 모든 숫자 중 102와 8에 각각 설정됩니다. ListView 항목에 대해이 변수의 값을 확인하면 (실행시 onCreate()
) 모두 0으로 표시됩니다. 그러나 FragmentActivity에 대한 ColorStrip을 만들 때 이상한 값이 할당됩니다.전역 정수가 잘못된 값으로 할당 됨
나는 그것이 FragmentActivity을 위해 그것을 만들 때 변수가 0 이외의 값을 할당지고 이유를 이해하지 않습니다
다음이 View
의 내 서브 클래스에 대한 모든 코드입니다.
public class ColorStrip extends View {
public ShapeDrawable mDrawable;
private static int hPx = 0;
private static int wPx = 0;
public ColorStrip(Context context, AttributeSet attrs) {
super(context, attrs);
mDrawable = new ShapeDrawable(new RectShape());
TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
R.styleable.ColorStrip, 0, 0);
try {
int color = a.getInt(R.styleable.ColorStrip_color, 0);
if (color != 0)
setColor(color);
} finally {
a.recycle();
}
}
protected void onDraw(Canvas canvas) {
if (wPx == 0)
wPx = getWidth();
if (hPx == 0)
hPx = getHeight();
mDrawable.setBounds(0, 0, wPx, hPx);
mDrawable.draw(canvas);
}
public void setColor(int color) {
mDrawable.getPaint().setColor(color);
}
}
여기 ListView의 XML :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<com.acedit.assignamo.ui.ColorStrip
android:id="@+id/assignment_list_color_strip"
android:layout_width="@dimen/color_strip_width"
android:layout_height="match_parent" />
과 FragmentActivity에 대한 XML :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ColorStrip="http://schemas.android.com/apk/res/com.acedit.assignamo"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.acedit.assignamo.ui.ColorStrip
android:id="@+id/assignment_view_color_strip"
android:layout_width="match_parent"
android:layout_height="@dimen/assignment_view_color_strip_height" />
왜 변수가 이상한 값이 할당되고있다?
정적으로 선언하지 마십시오. – auselen
@auselen Aha! 이봐. 답으로 써 주시면 받아 드리겠습니다. – mightimaus
나는 왜 내가 처음에 그것들을 정적이라고 선언했는지 의아해하고있다 ... 효율성상의 이유로 ... – mightimaus