2017-01-05 4 views
1

私はどこかで愚かな間違いがあることを知っていますが、私はどこを見つけることができません。 私は、コンテナのコー​​ドを以下ました:Androidスタジオはカスタムコンテナ内にビューを置くことを拒否します

public class ImagesView extends LinearLayout { 
public ImagesView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

public ImagesView(Context context) { 
    super(context); 
} 
@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    int widthMode = MeasureSpec.getMode(widthMeasureSpec); 
    int widthSize = MeasureSpec.getSize(widthMeasureSpec); 
    int heightMode = MeasureSpec.getMode(heightMeasureSpec); 
    int heightSize = MeasureSpec.getSize(heightMeasureSpec); 
    float ar=(float)896/768; 
    this.setMeasuredDimension(widthSize, (int) (widthSize/5*(1f/ar))); 
    //this.setMeasuredDimension(1280,240); 
} 
} 

レイアウトのxml:アンドロイドスタジオで

<?xml version="1.0" encoding="utf-8"?> 
<com.rhyboo.net.test.ImagesView xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@android:color/holo_red_light"> 

<Button 
    android:text="Button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/button3" 
    android:layout_weight="1" /> 
</com.rhyboo.net.test.ImagesView> 

私は私のコンテナのサイズが正しく設定されて見ることができますが、私は、コンテナの中に何かを追加する場合、それはのように子ビューを表示しますゼロサイズ: zero-sized child view

私は間違っていますか?

+0

完全なレイアウトコードを入力してください。 – MemLeak

+0

ええ、私の最初の投稿を編集しました。 – undefined

答えて

2

あなたのonMeasureはsuper.onMeasure ...を呼び出さないので、chilrenは測定されません。また、親のonMeasureを呼び出すときには、幅と高さのどちらかにモードを正確に設定したいと思うかもしれません。

+0

はい、ありがとうございました.BTWコンテナにモードを設定するにはどうすればよいですか? – undefined

+0

MeasureSpec.makeMeasureSpec.T再び! – undefined

関連する問題