在ImageView中导入图像

时间:2014-09-14 20:56:23

标签: java android

我在java片段中有这个代码,你可以看到我已经在我的每个项目中导入了textview。我没有尝试为每个项目导入imageview,例如:

问题是解决的。见下文。

    img1 = (ImageView) rootView.findViewById(R.id.img1);
    img1.setBackgroundResource(R.drawable.pickmeup);

以下是新代码:

package info.androidhive.tabsswipe;

import com.sothree.slidinguppanel.SlidingUpPanelLayout;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.ImageView;
import dk.appografen.joeandthejuice.R;

public class MenuDetailsFragment extends Fragment {

    TextView textView;
    View rootView;
    ImageView img1;
    private SlidingUpPanelLayout mLayout;

    //Juice

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle          savedInstanceState)             {
        String name = getArguments().getString("name");
        if (name.equals("PICK ME UP")) {
            rootView = inflater.inflate(R.layout.menu_details_slide, null, false);
            mLayout = (SlidingUpPanelLayout) rootView.findViewById(R.id.sliding_layout);
            textView = (TextView) rootView.findViewById(R.id.textView1);
            textView.setText("" + name + "");
            textView = (TextView) rootView.findViewById(R.id.textView2);
            textView.setText("Pris");
            textView = (TextView) rootView.findViewById(R.id.textView3);
            textView.setText("40 / 50 kr");

            textView = (TextView) rootView.findViewById(R.id.textView5);
            textView.setText("Ingredienser");
            textView = (TextView) rootView.findViewById(R.id.textView6);
            textView.setText("(Jordbær, banan, æble)");
            img1 = (ImageView) rootView.findViewById(R.id.img1);
            img1.setBackgroundResource(R.drawable.pickmeup);


        } else if (name.equals("ENERGIZER")) {
            rootView = inflater
            .inflate(R.layout.menu_details_slide, null, false);
            mLayout = (SlidingUpPanelLayout) rootView.findViewById(R.id.sliding_layout);
            textView = (TextView) rootView.findViewById(R.id.textView1);
            textView.setText("" + name + "");
            textView = (TextView) rootView.findViewById(R.id.textView2);
            textView.setText("Pris");
            textView = (TextView) rootView.findViewById(R.id.textView3);
            textView.setText("40 / 50 kr");

            textView = (TextView) rootView.findViewById(R.id.textView5);
            textView.setText("Ingredienser");
            textView = (TextView) rootView.findViewById(R.id.textView6);
            textView.setText("(Rød grape, æble, ingefær) ");
        }else {
            rootView = inflater
            .inflate(R.layout.menu_details_slide, null, false);
            textView = (TextView) rootView.findViewById(R.id.textView1);
            textView.setText("" + name + "");
            textView = (TextView) rootView.findViewById(R.id.textView2);
            textView.setText("Adresse");
            textView = (TextView) rootView.findViewById(R.id.textView3);
            textView.setText("Test");
            textView = (TextView) rootView.findViewById(R.id.textView4);
            textView.setText("9000 Aalborg");
            textView = (TextView) rootView.findViewById(R.id.textView5);
            textView.setText("Åbningstider");
            textView = (TextView) rootView.findViewById(R.id.textView6);
            textView.setText("Mandag - Fredag:");
            textView = (TextView) rootView.findViewById(R.id.textView7);
            textView.setText("07.30-20.00");
            textView = (TextView) rootView.findViewById(R.id.textView8);
            textView.setText("Lørdag:");
            textView = (TextView) rootView.findViewById(R.id.textView9);
            textView.setText("07.30-20.00");
            textView = (TextView) rootView.findViewById(R.id.textView10);
            textView.setText("Søndag::");
            textView = (TextView) rootView.findViewById(R.id.textView11);
            textView.setText("07.30-20.00");
    img1 = (ImageView) rootView.findViewById(R.id.img1);
    img1.setBackgroundResource(R.drawable.pickmeup);
        }
        return rootView;

    }
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        getView().setFocusableInTouchMode(true);
        getView().requestFocus();
        getView().setOnKeyListener(new OnKeyListener() {

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    Log.e("inside","keyevent");
                    if (mLayout != null && mLayout.isPanelExpanded() || mLayout.isPanelAnchored()) {
                        mLayout.collapsePanel();
                        Log.e("inside","expaned");
                        return true;
                    }

                }
                Log.e("inside","not expaned");
                return false;
            }
        });
        super.onViewCreated(view, savedInstanceState);
    }
}

2 个答案:

答案 0 :(得分:0)

也许你应该尝试这样的事情:

imageView.setImageResource(R.drawable.randomimage);

答案 1 :(得分:0)

试一试

mImageView.setImageDrawable(getResources().getDrawable(R.drawable.randomimage));