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からpicを表示したくないので、LinearLayoutから表示したいと思います。

4

1 に答える 1

0

ビューを追加するときは、LayoutParams を指定してレイアウトでのビューの表示方法を構成できる addView のバージョンを使用する必要があります。LayoutParamsを参照してください。

于 2010-08-06T20:04:27.410 に答える