当我设置layout_weight =" 1"时,如何在自定义布局中获得高度?在纵向LinearLayout
?
这是我的代码:
<LinearLayout
android:id="@+id/timer_layout1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<com.zzb.resume.views.DotView
android:id="@+id/timer_line1"
android:layout_width="2dp"
android:layout_height="30dp" />
<ImageView
android:id="@+id/timer_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/timer_line1"
android:src="@mipmap/time2"/>
<com.zzb.resume.views.DotView
android:id="@+id/timer_line2"
android:layout_width="2dp"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
在DotView中我得到height = 0。如何在layout_weight =&#34; 1&#34;?
时获得高度DotView.java:
public class DotView extends View {
private Paint p;
private int height;
private int dash;
public DotView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public DotView(Context context) {
super(context);
init(context);
}
private void init(Context context) {
p = new Paint(Paint.ANTI_ALIAS_FLAG);
p.setStyle(Style.FILL);
p.setColor(context.getResources().getColor(R.color.color_divide));
Log.d("TAG", "--getMeasuredHeight>>" + height);
Log.d("TAG", "--getHeight>>" + getHeight());
final float scale = context.getResources().getDisplayMetrics().density;
dash = (int) (6 * scale + 0.5f);
ViewTreeObserver viewTreeObserver = getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
getViewTreeObserver().removeOnGlobalLayoutListener(this);
height = getMeasuredHeight();
Log.d("TAG", "--onGlobalLayout>>" + height);
}
});
}
viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
getViewTreeObserver().removeOnPreDrawListener(this);
height = getMeasuredHeight();
Log.d("TAG", "--onPreDraw>>" + height);
return true;
}
});
}
@Override
protected void onDraw(Canvas canvas) {
if(height>10){
for(int i=0;i<height;i+=dash){
canvas.drawLine(0, i, 0, i+=dash, p);
}
}
super.onDraw(canvas);
}
@Override
public void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
height=h;
p.setStrokeWidth(height);
this.postInvalidate();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
height = MeasureSpec.getSize(heightMeasureSpec);
Log.d("TAG", "--onMeasure>>" + height);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
显示Logcat:所有结果均为0。
答案 0 :(得分:0)
你不应该在onCreate()
中获得高度,你可以在视图启动后设置一个监听器。以下是您可以看到的代码:
ViewTreeObserver vto = imageView.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
vto.removeOnPreDrawListener(this);
int height = imageView.getMeasuredHeight();
int width = imageView.getMeasuredWidth();
return true;
}
});
我希望这对你有所帮助。
答案 1 :(得分:0)
to achieve this you need to give weightsum in linear layout
<LinearLayout
android:id="@+id/timer_layout1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:weightSum="2"
android:orientation="vertical">
<com.zzb.resume.views.DotView
android:id="@+id/timer_line1"
android:layout_width="2dp"
android:layout_height="30dp" />
<ImageView
android:id="@+id/timer_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/timer_line1"
android:src="@mipmap/time2"/>
<com.zzb.resume.views.DotView
android:id="@+id/timer_line2"
android:layout_width="2dp"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>