我如何从自定义视图中获取宽度和高度尺寸?
我有一个自定义视图...
package nan.salsa.goal.customview;
import android.R;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
public class DayView extends View {
private static String TAG="DayView";
private ShapeDrawable mDrawable;
public DayView(Context context) {
super(context);
}
public DayView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public DayView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public void init() {
int x = 10;
int y = 10;
int width = 300;
int height = 300;
mDrawable = new ShapeDrawable(new RectShape());
mDrawable.getPaint().setColor(0xff74AC23);
mDrawable.setBounds(x, y, x + width, y + height);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
setBackgroundColor(R.color.black);
mDrawable.draw(canvas);
}
}
使用这个简单的配置文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#E06F00">
<nan.salsa.goal.customview.DayView android:id="@+id/dayView"
android:layout_height="match_parent"
android:layout_width="fill_parent" />
</LinearLayout>
在我看来,我想动态设置矩形的宽度...
我尝试过:
View parent = getParent();
int width = parent.getWidth();
// 抛出 nullPointerException 因为父级为 null ...
并且:
getRootView().getWidth();
// in这种情况下没有空指针异常,但方法返回 0:
我该怎么办?
问候
安东尼奥·穆塞拉
i have a Custom view ...
package nan.salsa.goal.customview;
import android.R;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
public class DayView extends View {
private static String TAG="DayView";
private ShapeDrawable mDrawable;
public DayView(Context context) {
super(context);
}
public DayView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public DayView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public void init() {
int x = 10;
int y = 10;
int width = 300;
int height = 300;
mDrawable = new ShapeDrawable(new RectShape());
mDrawable.getPaint().setColor(0xff74AC23);
mDrawable.setBounds(x, y, x + width, y + height);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
setBackgroundColor(R.color.black);
mDrawable.draw(canvas);
}
}
with this simple configuration file :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#E06F00">
<nan.salsa.goal.customview.DayView android:id="@+id/dayView"
android:layout_height="match_parent"
android:layout_width="fill_parent" />
</LinearLayout>
In my view i want to dinamically set width of my rectangle ...
i have tried :
View parent = getParent();
int width = parent.getWidth();
// throw nullPointerException because parent is null ...
and also:
getRootView().getWidth();
// in this case no null pointerException but method return 0:
How i can do ?
regards
Antonio Musella
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过将此方法添加到您的自定义视图类中:
By adding this method to your custom view class: