简体   繁体   中英

Programmatically setting the layout_weight of a View

I want to be able to set the layout_weight of this View progrmmatically. How can I do this?

final View v = (View) view.findViewById(R.id.vote_bar);

I've tried this but it always sets it to maximum which is 100

v.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 5f));

Here is the layout

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/text1"
            android:layout_alignBottom="@+id/text1"
            android:weightSum="100"
            android:orientation="horizontal">

            <View
                android:id="@+id/vote_bar"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:background="#68b4ff"
                android:padding="10dp"/>

        </LinearLayout>

XML

<LinearLayout
            android:id="@+id/lldashboard"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignTop="@+id/text1"
            android:layout_alignBottom="@+id/text1"
            android:weightSum="100"
            android:orientation="horizontal">

            <CardView
                android:id="@+id/cvModule"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#68b4ff"
                android:padding="10dp"/>

               //other layouts

        </LinearLayout> 

Kotlin file get a height of view

val viewTreeObserver = lldashboard.viewTreeObserver
        viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                lldashboard.viewTreeObserver.removeOnGlobalLayoutListener(this)
                val height = lldashboard.measuredHeight  
            }
        })

set height to view

mBinding.cvModule.layoutParams.height = ((height / 5).toInt())

You should be able to do something like:

<LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/text1"
            android:layout_alignBottom="@+id/text1"
            android:weightSum="100"
            android:orientation="horizontal">

            <View
                android:id="@+id/vote_bar"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:background="#68b4ff"
                android:padding="10dp"/>

        </LinearLayout>
LinearLayout ll = findViewById(R.id.linearLayout)
ll.setWeightSum(2f);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM