【问题标题】:How do you access a custom view's android:layout_width and android:layout_height parameters?如何访问自定义视图的 android:layout_width 和 android:layout_height 参数?
【发布时间】:2012-03-10 00:25:54
【问题描述】:

如何访问自定义视图的 android:layout_width 和 android:layout_height 值?

示例 xml:

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

【问题讨论】:

    标签: android views


    【解决方案1】:

    你必须得到视图的布局参数:

    CustomButton.LayoutParams viewParams = yourView.getLayoutParams();
    

    然后你可以访问或修改宽度或视图:

    viewParams.height;
    viewParams.width;
    

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-02
      • 1970-01-01
      • 2015-01-27
      • 1970-01-01
      • 1970-01-01
      • 2011-12-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多