0

我在 drawable-mdpi 文件夹中有一个名为 c1.png 的图像。我想用 Java 把它放到布局中:

package ...;

import android.os.Bundle;
//import android.provider.MediaStore.Images;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.view.Menu;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends Activity 
{

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        LinearLayout layout = (LinearLayout)findViewById(getCurrentFocus().getId() );

        ImageView img = new ImageView(this);
        ViewGroup.LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        img.setLayoutParams(lp);
        img.setImageDrawable(getResources().getDrawable(R.drawable.c1));
        layout.addView(img);
        setContentView(layout);//Doesn't matter, if it's commented or not. The error still exists.
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


}

启动时我得到:“不幸的是,%application_name 已停止”。我看到很多例子,在 id 参数中使用 R.id.*,但它也停止了(比如 R.id.linearLayout1),或者 R 没有这样的常量(比如 R.id.c1)。我怎样才能做到这一点?

4

3 回答 3

1

你不需要做 setContentView(layout); 另外,更换:

findViewById(getCurrentFocus().getId() )

经过

findViewById(R.id.layoutId); //layoutId is the id of the linearLayout defined in your layout.xml file. And R would refer to your local resources folder path and not the android Resources path.

您的 xml 文件可能看起来像这样(这将是 main.xml):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    <LinearLayout
        android:id="@+id/layoutId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>
于 2013-03-06T04:25:20.327 回答
1
import android.app.Activity;
import android.view.Menu;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends Activity 
{

@Override
protected void onCreate(Bundle savedInstanceState) 
{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    LinearLayout layout = (LinearLayout)findViewById(R.id.layoutID ); // //layoutID is id of the linearLayout that defined in your main.xml file

    ImageView img = new ImageView(this);
    ViewGroup.LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    img.setLayoutParams(lp);
    img.setBackgroundResource(R.drawable.c1);
    layout.addView(img);

}
@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

希望这可以帮助。

于 2013-03-06T04:27:54.487 回答
0

使用代码:

 ImageView img = new ImageView(this);
 img.setImageResource(R.drawable.c1);
于 2013-03-06T04:46:57.927 回答