android - 如何在自身内部设置自定义视图高度的一半宽度?
问题描述
android如何LinearLayout
在自己内部设置自定义高度的一半。
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec / 2);
}
我使用此代码,但孩子的代码被切断了。
解决方案
public class CustomLinearLayout extends LinearLayout {
private static final float RATIO = 0.5f;
public CustomLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthSpec, int heightSpec) {
int customHeight = MeasureSpec.makeMeasureSpec(
(int)(MeasureSpec.getSize(widthSpec) * RATIO), MeasureSpec.EXACTLY);
super.onMeasure(widthSpec, customHeight);
}
}
推荐阅读
- javascript - ReactJS 在表的 tbody 内嵌套循环
- javascript - PHP - 我如何从查询字符串中取回(jquery)循环数组
- java - Apache Flink writeAsCsv 没有内容。
- php - 许多方法参数与批量参数
- python - 设置 WingIDE 以调试 Flask 项目
- java - 使用 Visual Studio Code 在 Java 中自动生成接口方法
- apache-nifi - NiFi:将数据从 Kafka 插入到 Cassandra 的最佳流程?
- javascript - 为什么我在打字稿中没有收到有关 StrictNullChecks 的警告
- r - 使用 mgcv::gam 进行非标准评估
- c# - 使用自定义属性保存设置