如何从不同的XML更改ImageView源?

时间:2016-10-14 16:58:50

标签: java android xml imageview imagesource

我想在弹出窗口中更改图像。我正在尝试更改test.xml中的ImageView图像源,而我的contentView是activity_main。当我试着打电话给imageView.setImageResource(android.R.drawable.ic_menu_help);时,它给了我nullpointerexception我意识到我必须使用inflate方法,但是当我使用它时它不能做我想要的。我知道有很多问题对此,但他们都没有真正帮助我。 这是activity_main.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/relative"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.android.popupwindow.MainActivity">

    <Button
        android:text="Test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

的test.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_blue_dark"
    android:id="@+id/test1"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linear1">

        <ImageView
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:src="@android:drawable/btn_star_big_on"
            android:id="@+id/imageView1"
            android:layout_weight="1"
             />
    </LinearLayout>

</RelativeLayout>

我的main.java:

public class MainActivity extends AppCompatActivity {

    private PopupWindow popupWindow;
    private RelativeLayout relativeLayout;

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

        Button test = (Button) findViewById(R.id.button);
        relativeLayout = (RelativeLayout) findViewById(R.id.relative);

        final View view1 = getLayoutInflater().inflate(R.layout.test, null);
        ImageView imageView = (ImageView) view1.findViewById(R.id.imageView1);
        imageView.setImageResource(android.R.drawable.ic_menu_help); //Image source is not changed in my popupWindow, 
                                                                     // but it is changed when I call relativeLayout.addView(view1);

        test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //relativeLayout.addView(view1); // <-- this does the trick, but its not what i want
                View container = getLayoutInflater().inflate(R.layout.test, null);

                popupWindow = new PopupWindow(container, android.app.ActionBar.LayoutParams.MATCH_PARENT, android.app.ActionBar.LayoutParams.WRAP_CONTENT, true);
                popupWindow.showAtLocation(relativeLayout, Gravity.BOTTOM, 0, 0);
                container.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View view, MotionEvent motionEvent) {
                        popupWindow.dismiss();
                        return true;
                    }
                });
            }
        });
    }
}

1 个答案:

答案 0 :(得分:1)

您正在夸大两个不同的观点。显然,你在一个人做的事情对另一个人没有影响。您应该将弹出视图设置为最初膨胀的视图:

popupWindow = new PopupWindow(view1,
        RelativeLayout.LayoutParams.MATCH_PARENT, 
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        true);