[英]Inflate child with 0dp layout width & weight (programmatically)
簡單:我想給1個寬度為0dp的孩子充氣。
父XML:
<com.example.KeyPad // extend LinearLayout
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:
<RelativeLayout
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">
<TextView ... />
</RelativeLayout>
然后通過以下方式添加孩子:
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);
如何為孩子充氣0dp /體重? 當然,如您所見,我已經定義了父級的weight_sum。
您使用了LinearLayout.LayoutParams
但是按鈕的父級是RelativeLayout
。
嘗試這個 :
KeyButton bt = new KeyButton(context);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(0, RelativeLayout.LayoutParams.WRAP_CONTENT,1.0f);
Parent.addView(bt, lp);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.