java在编程上等效于创建以下XML
文件的内容:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout />
</LinearLayout>
我尝试这样做,但有些LinearLayout
如何延伸至MATCH_PARENT
// child is the RelativeLayout in the above example:
LinearLayout parent = new LinearLayout(getContext());
LinearLayout.LayoutParams childLayoutParams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
parent.addView(child, -1, childLayoutParams);
答案 0 :(得分:1)
试试这个
LinearLayout parent = new LinearLayout(getContext());
LayoutParams parentLayoutParams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
parent.setLayoutParams(parentLayoutParams);
然后将其添加到屏幕
outerlayout.addView(parent);
答案 1 :(得分:0)
您设置的LayoutParams
适用于RelativeLayout
,不适用于LinearLayout
。我会打赌,LinearLayout
使用MATCH_PARENT
作为默认参数。当您将parent
添加到Window
或root ViewGroup
时,您应该创建一组正确的LayoutParams
并指定WRAP_CONTENT
。