0

我正在尝试使用 setContentView(R.layout.main); 从活动中显示主屏幕;然后显示如下图像:

公共类 TryGraph 扩展 Activity {

/** Called when the activity is first created. */


public LinearLayout mLinearLayout;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    // Create a LinearLayout in which to add the ImageView
    mLinearLayout = new LinearLayout(this);

    // Instantiate an ImageView and define its properties
    ImageView i = new ImageView(this);
    i.setImageResource(R.drawable.face3);
    i.setAdjustViewBounds(true); // set the ImageView bounds to match the Drawable's dimensions

    mLinearLayout.addView(i);
    setContentView(mLinearLayout);

}
}

main.xml是这样的

           <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

            android:orientation="vertical"

            android:layout_width="fill_parent"

             android:layout_height="fill_parent"

             >
            <TextView  
             android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
               android:text="@string/hello"
                 />

         <Button android:text="button" android:id="@+id/Button01" 

          android:layout_width="wrap_content"               
       android:layout_height="wrap_content"></Button>

该按钮被图片隐藏,因为它被绘制在按钮的顶部。如何将图片动态放置在不同的位置,以便同时显示按钮和图片。注意:我不想从 xml 显示图片,我想从 LinearLayout 显示它。

4

1 回答 1

0

添加视图时,您应该使用允许您提供 LayoutParams 来配置视图在布局中的显示方式的 addView 版本。请参阅LayoutParams

于 2010-08-06T20:04:27.410 回答