我正在按照本教程爲Android創建應用程序並遇到問題。免責聲明:我幾乎不知道Java或Eclipse,所以請耐心等待。如何將ImageView添加到根佈局?
我創建了一個位圖,我把它放到一個ImageView(?)中,現在教程說要將ImageView添加到root_layout,但公平起見,我不知道root_layout是什麼(我谷歌搜索了一些,但找不到正確答案)。此外,Eclipse給了我'layoutroot無法解決或不是一個領域'的錯誤,我不知道如何解決。我的問題是,我如何讓圖像顯示在屏幕上?在此先感謝:-)
這裏是我的(全)代碼:
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class ShowImage extends ActionBarActivity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.show_image, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// shows the activity
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_image);
try {
// load large image from resources
Bitmap game_image = BitmapFactory.decodeResource(this.getResources(), R.drawable.sample_0);
// create cropped image from loaded image
Bitmap cropped = Bitmap.createBitmap(game_image, 0, 0, 100, 100);
// no longer need larger image
game_image.recycle();
// create ImageView to display image
ImageView imageView = new ImageView(this);
imageView.setImageBitmap(cropped);
// add ImageView to root layout
LinearLayout root = (LinearLayout)this.findViewById(R.id.root_layout);
root.addView(imageView);
}
// catch comes here
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
// shows GamePlay-activity after three seconds
Intent intent = new Intent(ShowImage.this, GamePlay.class);
ShowImage.this.startActivity(intent);
ShowImage.this.finish();
}
}, 3000);
}
}
新增的.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="nl.mprog.projects.nPuzzle10206353.ShowImage" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Wait three seconds..." />
</RelativeLayout>
安置自己的'activity_show_image'? – 2014-11-24 10:22:05
嘗試使用addContentView(imageView)代替root.addView(imageView)。 – 2014-11-24 10:22:20
你的activity_show_image.xml文件在哪裏? – 2014-11-24 10:30:53