如何根据Android中的值设置视图的宽度

huangapple go评论47阅读模式
英文:

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>

如何根据Android中的值设置视图的宽度

英文:

如何根据Android中的值设置视图的宽度

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.

             &lt;RelativeLayout
                android:layout_width=&quot;match_parent&quot;
                android:layout_height=&quot;match_parent&quot;
                android:layout_marginTop=&quot;20dp&quot;
                android:layout_below=&quot;@+id/headerproject&quot;&gt;

                &lt;View
                    android:layout_width=&quot;match_parent&quot;
                    android:layout_height=&quot;5dp&quot;
                    android:background=&quot;@color/grey&quot;&gt;
                    
                &lt;/View&gt;
                
                &lt;View
                    android:layout_width=&quot;200dp&quot;
                    android:layout_height=&quot;5dp&quot;
                    android:background=&quot;@color/colorPrimary&quot;
                    &gt;
                    
                &lt;/View&gt;
            &lt;/RelativeLayout&gt;

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

huangapple
  • 本文由 发表于 2020年8月3日 13:18:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63224101.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定