首页 > 解决方案 > 如何根据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>

标签: javaandroidandroid-studio

解决方案


您可以使用 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);


    
    

推荐阅读