如何从不同 XML 更改 ImageView 源?

How to change ImageView source from different XML?

我想更改弹出窗口中的图像。我正在尝试更改 test.xml 中的 ImageView 图像源,而我的 contentView 是 activity_main。当我尝试只调用 imageView.setImageResource(android.R.drawable.ic_menu_help); 它给了我 nullpointerexception 我意识到我必须使用膨胀方法,但是当我使用它时它没有做我 want.I 知道有很多关于此的问题,但其中 none 确实帮助了我。 这是 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;
                    }
                });
            }
        });
    }
}

您正在夸大两个单独的视图。显然,你在一个中所做的不会对另一个产生影响。您应该将弹出视图设置为您最初的膨胀视图:

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