簡體   English   中英

如何根據 android 中的值設置視圖寬度

[英]How to set width of view with respect to value in 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>

您可以使用ProgressBar但如果您真的需要 go 以這種方式,您可以設置綠色條的寬度,如:

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);


    
    

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM