繁体   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