Я пишу пользовательский View
объект, но я не могу заставить его измерить правильно. Глядя на исходный код View
, я подумал, что достаточно будет вызвать setMinimumHeight()
и setMinimumWidth()
(это действительно все, что мне нужно, минимальный размер, который должен соблюдать родительский макет). Вот мой код:
public class MonthView extends View {
private final int minCellSize = 24;
public MonthView(Context context) {
super(context);
}
public MonthView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MonthView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final float scale = getContext().getResources().getDisplayMetrics().density;
setMinimumHeight((int) (minCellSize * scale * 6));
setMinimumWidth((int) (minCellSize * scale * 7));
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.RED);
}
}
Довольно просто. Затем я вставляю его в LinearLayout
, что-то вроде этого:
<?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"
>
<com.foghina.adtp.MonthView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="I am below the monthview!"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
Однако MonthView
занимает весь экран, а TextView
не виден. Как правильно написать мой View
, чтобы он имел минимальную высоту / ширину при использовании wrap_content
?