2012-03-09 11 views
2

カスタムビューのandroid:layout_widthとandroid:layout_heightの値にはどのようにアクセスしますか?カスタムビューのandroid:layout_widthおよびandroid:layout_heightパラメータにはどのようにアクセスしますか?

サンプルXML:CustomButtonで

<com.example.CustomButton 
    android:id="@+id/btn" 
    android:layout_width="150dp" 
    android:layout_height="70dp" /> 

@Override 
protected void onMeasure(int widthSpec, int heightSpec) { 
    int measuredWidth = MeasureSpec.getSize(widthSpec); 
    int measuredHeight = MeasureSpec.getSize(heightSpec); 
    // How can we measure based on android:layout_width, android:layout_height? 
    // Currently this implementation measures off the parent's values 
    setMeasuredDimension(measuredWidth, measuredHeight); 
} 

答えて

4

あなたはビューのレイアウトのparamsを取得する必要があります。

CustomButton.LayoutParams viewParams = yourView.getLayoutParams(); 

をそしてあなたがアクセスしたり、幅を変更することができますまたは表示:

viewParams.height; 
viewParams.width; 

希望すると助かります!

+0

私はありがとうございました。 – Dude

関連する問題