0
我正在處理自定義視圖,它應該具有相同的高度和寬度。調整視圖的大小到給定的空間
我繪製它以下述方式:
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(getWidth()/2, getHeight()/2, getWidth()/2, mPaint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
widthMeasureSpec = heightMeasureSpec;
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
如果我僅添加一個視圖到屏幕上,寬度/高度是確定。但是,如果我將這些視圖中的兩個相鄰添加,則應調整視圖的大小(每個視圖:屏幕的半寬)。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<at.guger.widget.Light
android:id="@+id/lights_light1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="5dp" />
<at.guger.widget.Light
android:id="@+id/lights_light2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="5dp" />
</LinearLayout>
我該怎麼做?