簡體   English   中英

如何將ImageView添加到根布局?

[英]How do I add an ImageView to the root layout?

我正在按照本教程制作適用於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>

將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方法

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

在您可以添加圖像之后

Bitmap bitmap  = BitmapFactory.decodeResource(getResources(), R.drawable.image);
im.setImageBitmap(bitmap);

root_layout基本上是您要在其中添加新創建的View的xml中的現有布局。

在您的xml文件中,將id root_layout分配給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 id為root_layout

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.
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM