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()
ImageView
MyCustomLinearLayout