0

什么是 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);
4

2 回答 2

1

试试这个

LinearLayout parent = new LinearLayout(getContext());
LayoutParams parentLayoutParams = new LinearLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
parent.setLayoutParams(parentLayoutParams);

然后将其添加到屏幕上

outerlayout.addView(parent);
于 2012-04-11T00:27:32.037 回答
0

LayoutParams您设置的是,RelativeLayout而不是LinearLayout。我敢打赌,它被用作默认参数LinearLayoutMATCH_PARENT当您添加parentWindow或 根ViewGroup时,您应该创建一个适当的集合LayoutParams并指定WRAP_CONTENT.

于 2012-04-11T00:17:15.323 回答