我正在尝试在Textview和Button之间动态放置位图:
<TextView android:text="TextView" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:layout_width="wrap_content" android:orientation="vertical">
</LinearLayout>
<LinearLayout android:layout_width="fill_parent" android:id="@+id/linearLayout2" android:layout_height="fill_parent" android:orientation="vertical">
<Button android:layout_width="fill_parent" android:layout_gravity="fill_vertical" android:text="Button" android:layout_height="fill_parent" android:id="@+id/button1"> </Button>
</LinearLayout>
在我的主要活动中,我创建自定义视图并将其添加到LinearLayout1
mView = new myView(this);
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout1);
ll.addView(rView);
我的问题是,myView会占用它下面的所有可用空间。
我错过了什么?
Thnx提前。
答案 0 :(得分:2)
尝试为您的自定义视图动态设置布局参数。
l1.addView(rView, new LinearLayout.LayoutParams(100, LayoutParams.WRAP_CONTENT));
同样尝试动态设置左右位置
答案 1 :(得分:1)
添加视图时应添加LayoutParams:
ll.addView(rView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
如果您想将其添加到特定位置,请使用:addView(child, index)
。
希望这有帮助!