setImageResource在Android中不起作用

时间:2016-12-14 15:42:56

标签: java android

我是java的初学者,我想随机化几张图片,我使用这段代码

int[] cards = {R.drawable.i1, R.drawable.i2, R.drawable.i3};
Random n = new Random();
Resources resources = getResources();
imgRandom.setImageResource(cards[n]);

并且它不起作用因为 setImageResource 是红色且Android Studio说:无法解析符号'setImageResource'。有人可以帮帮我吗?

所有java代码:

    package eduard.alarmamotivationala;

import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    ImageView m_imgRandom;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public ImageView getM_imgRandom() { int[] cards = {R.drawable.i1, R.drawable.i2, R.drawable.i3};
        Random n = new Random();
        Resources resources = getResources();
        m_imgRandom.setImageResource(cards [3]);
        return m_imgRandom; }
}

所有XML代码:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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"
    android:background="#000000"
    tools:context="eduard.alarmamotivationala.MainActivity">

    <Button
        android:text="Porneste alarma"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:id="@+id/button" />

    <Button
        android:text="Opreste alarma"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:id="@+id/button2" />

    <TextView
        android:text="Inca nu ai setat alarma"
        android:textColor="#FFFFFF"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="11dp"
        android:id="@+id/textView" />

    <TimePicker
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleX="0.7"
        android:scaleY="0.7"
        android:headerBackground="@android:color/transparent"
        android:numbersSelectorColor="#027aed"
        android:id="@+id/timePicker"
        android:layout_marginTop="36dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />

    <ImageView
        android:id="@+id/imgRandom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType = "centerCrop" />
</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

int[] cards = {R.drawable.i1, R.drawable.i2, R.drawable.i3};
int n = new Random().nextInt(3);
imgRandom.setImageResource(cards[n]);

生成0到2的随机数

int n = new Random().nextInt(3);

从数组中取出随机图像并设置为图像

imgRandom.setImageResource(cards[n]);

多数人!!

答案 1 :(得分:0)

$("#container1,#container2").change(function() {
    $("#cont_seal_1").val($("#container1 option:selected").attr('data-seal'));
    $("#cont_seal_2").val($("#container2 option:selected").attr('data-seal'));
});

也试试,

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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"
    android:background="#000000"
    tools:context="eduard.alarmamotivationala.MainActivity">

    <Button
        android:text="Porneste alarma"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:id="@+id/button" />

    <Button
        android:text="Opreste alarma"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:id="@+id/button2" />

    <TextView
        android:text="Inca nu ai setat alarma"
        android:textColor="#FFFFFF"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="11dp"
        android:id="@+id/textView" />

    <TimePicker
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleX="0.7"
        android:scaleY="0.7"
        android:headerBackground="@android:color/transparent"
        android:numbersSelectorColor="#027aed"
        android:id="@+id/timePicker"
        android:layout_marginTop="36dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />

    <!--changed wrap_content to match_parent for getting the full screen image view-->

    <ImageView
        android:id="@+id/imgRandom"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType = "centerCrop" />
</RelativeLayout>

android:scaleType = "centerInside"

感受到变化。

在java文件中,

android:scaleType = "fitXY"

未经过测试。它有效!!