MyCustomLinearLayout拡張する a を表示しようとしていますLinearLayout。属性で膨らませてMyCustomLinearLayoutいandroid:layout_height="match_parent"ます。私が欲しいのはImageView、これに を表示することMyCustomLinearLayoutです。これの高さは で、幅は高さと等しくなければなりませんImageView。match_parentメソッドをオーバーライドすることでこれを達成しようとしていますonMeasure()。何が起こるかとMyCustomLinearLayoutいうと、本来のように正方形になりますが、ImageViewは表示されません。
私の使用したコードの下。これは私の問題の非常に単純化されたバージョンであることに注意してください。はImageView、より複雑なコンポーネントに置き換えられます。
public MyCustomLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.view_myimageview, this);
setBackgroundColor(getResources().getColor(R.color.blue));
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
setMeasuredDimension(height, height);
}
view_myimageview.xmlファイル:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<ImageView
android:id="@+id/view_myimageview_imageview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher" />
</merge>
したがって、onMeasure()メソッドをオーバーライドすると表示されず、メソッドをImageViewオーバーライドしないと表示されますが、幅が小さすぎるため、小さすぎます。onMeasure()ImageViewMyCustomLinearLayout