gpt4 book ai didi

android - 你如何访问自定义 View 的android :layout_width and android:layout_height parameters?

转载 作者:搜寻专家 更新时间:2023-11-01 08:11:00 25 4
gpt4 key购买 nike

如何访问自定义 View 的 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);
}

最佳答案

您必须获取 View 的布局参数:

CustomButton.LayoutParams viewParams = yourView.getLayoutParams();

然后您可以访问或修改宽度或 View :

viewParams.height;
viewParams.width;

希望对您有所帮助!

关于android - 你如何访问自定义 View 的android :layout_width and android:layout_height parameters?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9637446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com