我正在按照本教程制作适用于Android的应用并遇到问题。免责声明:我对Java或Eclipse几乎一无所知,所以请耐心等待。
我创建了一个位图,我将其放入ImageView(?),现在教程说要将ImageView添加到root_layout,但公平地说,我不知道root_layout是什么(我用Google搜索了一些,但是找不到合适的答案)。另外,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>
答案 0 :(得分:0)
将imageView添加到xml:
<ImageView android:id="@+id/mImageView"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:contentDescription="solecito"
android:layout_weight = "1"
android:layout_gravity="center"
/>
然后在onCreate Method
上添加它ImageView im = (ImageView)findViewById(R.id.mImageView);
添加图像后
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
im.setImageBitmap(bitmap);
答案 1 :(得分:0)
root_layout
基本上是xml中要添加新创建的View的现有布局。
将id root_layout
分配给xml文件中的RelativeLayout。
或强>
使用root_layout
id。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root_layout"
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中使用它时:
RelativeLayout root = (RelativeLayout)this.findViewById(R.id.root_layout);
root.addView(imageView);
imageView
会添加到标识为RelativeLayout
root_layout
答案 2 :(得分:0)
package com.tommymacwilliam.androidwalkthroughapp3;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.ImageView;
import android.widget.Toast;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class AndroidWalkthroughApp3 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
// load large image from resources
Bitmap background = BitmapFactory.decodeResource(this.getResources(), R.drawable.puzzle_0);
// create cropped image from loaded image
Bitmap cropped = Bitmap.createBitmap(background, 0, 0, 230, 230);
// no longer need larger image
background.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);
int screenWidth = this.getResources().getDisplayMetrics().widthPixels;
Toast.makeText(this, String.valueOf(screenWidth), Toast.LENGTH_LONG).show();
}
catch (OutOfMemoryError e) {
// uh oh.
}
}
}