2014-11-24 112 views
0

我正在按照本教程爲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> 
+0

安置自己的'activity_show_image'? – 2014-11-24 10:22:05

+2

嘗試使用addContentView(imageView)代替root.addView(imageView)。 – 2014-11-24 10:22:20

+0

你的activity_show_image.xml文件在哪裏? – 2014-11-24 10:30:53

回答

0

root_layout基本上是在XML中,要添加新創建的視圖的現有佈局。

將ID root_layout指定給xml文件中的RelativeLayout。

OR

保持一個新的佈局裏面有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> 

所以現在,當您在活動中使用這樣的:

RelativeLayout root = (RelativeLayout)this.findViewById(R.id.root_layout); 
root.addView(imageView); 

imageView將被添加到RelativeLayout ID爲root_layout

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" 
/> 

然後將其添加到您的創建方法

ImageView im = (ImageView)findViewById(R.id.mImageView); 

後,您可以添加圖像

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image); 
im.setImageBitmap(bitmap); 
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. 
     } 
    } 
}