英文:
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>

英文:
How to change the second view length according to value. If the first one value is 100 then the width is match_parent. If the second one value is 60 then how to set the width of that according to that one.
             <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>
答案1
得分: 0
你可以使用 ProgressBar,但如果你真的需要继续这种方式,你可以像这样设置绿色条的宽度:
View viewGray, viewGreen;
int currentValue, totalValue;
//一些相关的代码
viewGray.postDelayed(new Runnable() {
    @Override
    public void run() {
        int parentWidth = viewGray.getWidth();
        int viewGrayWidth = (int)(parentWidth * (currentValue / (float)totalValue));
        viewGreen.getLayoutParams().width = viewGrayWidth;
    }
}, 50);
英文:
You can use a ProgressBar but if you really need to go on this way, you can set the green bar width like:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论