如何使用java在布局中放置图像?安卓
我在drawable mdpi文件夹中有一个名为c1.png的图像。我想通过以下方式将其放到Java布局中:如何使用java在布局中放置图像?安卓,java,android,android-image,Java,Android,Android Image,我在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 andro
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;
}
}
当启动它时,我得到:“不幸的是,%的应用程序\u名称已停止”。我看到很多例子,在id参数中使用R.id.*,但它也停止了(比如R.id.linearLayout1),或者R没有这样的常量(比如R.id.c1)。
我怎样才能做到这一点?您不需要执行setContentView(布局);此外,替换:
findViewById(getCurrentFocus().getId() )
借
xml文件可以如下所示(这将是main.xml):
希望这有帮助。使用代码:
ImageView img = new ImageView(this);
img.setImageResource(R.drawable.c1);
如果事实上,我的布局看起来像RelativeLayout,而不是Linear main会有RelativeLayout,在里面你会有带有Id的linearLayout。检查我编辑的ans。好吧,它工作了,但只有PAD的代码。我必须以不同的方式处理图像。谢谢!
<?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>
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;
}
}
ImageView img = new ImageView(this);
img.setImageResource(R.drawable.c1);