自定义控件设置layout_weight

文章详细介绍了如何在自定义控件中实现设置权重的方法,适用于多个控件放置于LinearLayout布局场景,通过重写控件内部方法来调整布局权重,提升界面设计灵活性。

自定义一个控件,继承view。当多个这个控件被放入linearlayout中时,发现不能使用 myView1.setWeight()。

此时需要在自定义控件中编写一个这样的方法:

    public void setWeight(float weight) {
        LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT, weight);
        setLayoutParams(param);
    }

就可在外部调用 

 myView1.setWeight(1);
 myView2.setWeight(1);

 

转载于:https://www.cnblogs.com/fangtest/p/3925521.html

<!-- 在原有布局基础上添加校准面板 --> <LinearLayout android:id="@+id/calibrationPanel" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="#80000000" android:padding="16dp" android:visibility="gone" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="颜色校准" android:textSize="20sp" android:textColor="@android:color/white" android:textStyle="bold"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="8dp"> <Button android:id="@+id/btnCaptureWhite" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="白色校准" android:backgroundTint="#9E9E9E"/> <Button android:id="@+id/btnCaptureBlack" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="黑色校准" android:backgroundTint="#212121" android:layout_marginStart="8dp"/> </LinearLayout> <Button android:id="@+id/btnCaptureCustom" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="自定义校准" android:layout_marginTop="8dp"/> <Button android:id="@+id/btnResetCalibration" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="重置校准" android:backgroundTint="#F44336" android:textColor="@android:color/white" android:layout_marginTop="8dp"/> <TextView android:id="@+id/tvCalibrationStatus" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="校准状态: 未校准" android:textSize="14sp" android:textColor="#CCCCCC" android:layout_marginTop="8dp"/> </LinearLayout> 此布局是之前回复的答案,但是没看到btnCalibrationToggle这个控件id,,
最新发布
07-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值