2011-11-02 78 views
1

按钮后没有表现出我想要动态地把一个位图一个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)); 

类似尝试设置左,右位置动态

+0

非常感谢!这是我7小时搜索的解决方案! – Schwoabaseggele

+0

然后接受这个答案 – Karthi

1

添加视图时,您应该添加的LayoutParams:你想将其添加到您应该在特定的位置

ll.addView(rView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

万一使用:addView(child, index)

希望这会有所帮助!

+0

嗨Dimitris,我刚刚检查了你的解决方案,但我的问题依然存在。因为我需要我的视图(其中包含一个正方形的位图)在宽度*宽度大小下面的代码给了我正确的画面:'ll.addView(rView,new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,((WindowManager)getApplicationContext())。 getSystemService(Context.WINDOW_SERVICE))getDefaultDisplay()的getWidth()));' – Schwoabaseggele