我该如何从Kotlin代码动态地为安卓中的LinerLayout设置layout_weight属性的值?
var orange : LinearLayout = findViewById(R.id.orangeLineFiveStars) as LinearLayout
这段代码是否正确?[它在我的应用程序中无法工作]
var orangeParams : LinearLayout.LayoutParams = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
orangeParams.weight = 0.33f
orange.layoutParams = orangeParams
这段代码也不起作用
var orange.layoutParams = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,0.33f)
XML :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.sanke.ilafedoseev.raitmyworkstatistic.MainActivity">
<LinearLayout
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/orangeLineFiveStars"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="4dp"
android:background="#febe40"
android:orientation="horizontal"/>
<LinearLayout
android:id="@+id/whiteLineFiveStars"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"/>
</LinearLayout>
</LinearLayout>
var orangeParams: LinearLayout.LayoutParams = LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 0.33f)
- eugeneek