android增布局,使用 0dp 个布局宽度&重量增加子级( 以编程方式)_android-view_开发99编程知识库...

簡單:我想用 1個寬度為 0dp的子元素來。

父 xml:

android:layout_width="match_parent"

android:layout_height="match_parent"

android:weightSum="4"//for the children

android:layout_weight="1"//this is for its parent

android:clickable="true"

android:background="@color/MidnightBlue">

子類別:public class KeyButton extends RelativeLayout implements View.OnClickListener{

public KeyButton(Context c ) {

super(c);

RelativeLayout v = (RelativeLayout) LayoutInflater.from(c).inflate(R.layout.key_button, this, true);

}

}

}

它使用 R.layout.key_button xml:

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_weight="1"

android:layout_width="0dp"

android:background="@android:color/holo_red_dark"

android:layout_height="wrap_content">

子對象正被添加:Parent.addView(new KeyButton(context) );

問題在於,android:layout_weight 並不想採取行動,而且子級的layout_width仍在" 0dp"中。 如果我改變寬度到 50dp,我可以看到正確膨脹的孩子。

在添加時還嘗試以編程方式添加參數:KeyButton bt = new KeyButton(context);

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT,1.0f);

bt.setLayoutParams(lp);

Parent.addView(bt);

如何用 0 dp/體重來膨脹孩子? 當然,就像你看到的,我已經定義了父對象的weight_sum 。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值