Как получить доступ к параметрам Android: layout_width и android: layout_height? - PullRequest
2 голосов
/ 09 марта 2012

Как получить доступ к Android вашего пользовательского представления: layout_width и android: layout_height values?

Пример xml:

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

В CustomButton:

@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);
}

1 Ответ

4 голосов
/ 09 марта 2012

Вы должны получить параметры макета вида:

CustomButton.LayoutParams viewParams = yourView.getLayoutParams();

И затем вы можете получить доступ или изменить ширину или вид:

viewParams.height;
viewParams.width;

Надеюсь, это поможет!

...