java - 如何根据android中的值设置视图宽度
问题描述
如何根据值更改第二个视图长度。如果第一个值为 100,则宽度为 match_parent。如果第二个值为 60,那么如何根据该值设置其宽度。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:layout_below="@+id/headerproject">
<View
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="@color/grey">
</View>
<View
android:layout_width="200dp"
android:layout_height="5dp"
android:background="@color/colorPrimary"
>
</View>
</RelativeLayout>
解决方案
您可以使用 aProgressBar
但如果您真的需要这样做,您可以设置绿色条的宽度,如:
View viewGray, viewGreen;
int currentValue, totalValue;
//Some relevant code
viewGray.postDelayed(new Runnable() {
@Override
public void run() {
int parentWith = viewGray.getWidth();
int viewGrayWith = parentWith * (currentValue/(float)totalValue);
viewGreen.getLayoutParams().width = viewGrayWith;
}
},50);
推荐阅读
- javascript - 如何保持 $.getJSON 响应有序?
- javascript - Fabric.js - problems with pattern fill on fabric.Text using patternTransform
- python - 是否存在可以对时间序列数据进行全自动arima 的python 模型?
- javascript - 导航到另一个页面时,Safari 上未显示页面加载覆盖
- android - 使用 WorkManager 更新 Room 中的数据库条目
- c# - 服务器端事件并不总是及时发送到客户端
- sql - 如何从当前日期获取以前的记录?
- facebook - 登录后立即在 Facebook 客户聊天插件中向用户发送消息
- c++ - 队列为空时 priority_queue pop() 和 top() 的 C++ 行为
- linux - 杯子打印页面后运行脚本