gpt4 book ai didi

android - getLayoutParams.width 与 getWidth()

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

我有以下看法:

<View
android:id="@+id/divider"
android:layout_height="2dp"
android:background="#ffffff"
android:layout_below="@+id/timer"
android:layout_width="wrap_content" />

我正在我的项目中实现一个计时器。因此,每秒调用一次函数 onTick():

public void onTick(long millisUntilFinished) {
secsPassed++;
minsPassed = (secsPassed-1)/60;
View dividerWhite = findViewById(R.id.dividerWhite);
View divider = findViewById(R.id.divider);
Log.i("divider.getWdith() = ", divider.getWidth()-secsPassed + "");
Log.i("layout width ", divider.getLayoutParams().width + " ");
//further code to implement a timer

问题是,divider.getLayoutParams().width 的值为 -1,而 divider.getWidth() 的值是正确的(即剩余秒数)

我需要随着时间的流逝动态更改 View 的宽度。 divider.setLayoutParams().width 是一个不错的选择。但是,它并没有改变布局宽度,它仍然是 -1

最佳答案

getLayoutParams().width 当宽度为 MATCH_PARENT 时返回 -1。 getLayoutParams().width 可以是 FILL_PARENT(在 API 级别 8 中被 MATCH_PARENT 取代)或 WRAP_CONTENT 或一个确切的大小。但是 getWidth() 总是返回以像素为单位的宽度。

关于android - getLayoutParams.width 与 getWidth(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34126843/

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