【问题标题】:How to change ImageView source from different XML?如何从不同的 XML 更改 ImageView 源?
【发布时间】:2016-10-14 16:58:49
【问题描述】:

我想更改弹出窗口中的图像。我正在尝试更改 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;
                    }
                });
            }
        });
    }
}

【问题讨论】:

  • 尝试使用setImageDrawable(Drawable d) 看看是否有效

标签: java android xml imageview imagesource


【解决方案1】:

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

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

【讨论】:

  • 谢谢。我想我并没有真正理解 inflate 方法,我以为我的第一个 inflate 改变了测试布局
  • 有什么方法可以更改我的 Java 代码中的测试布局,以便在创建 View container = getLayoutInflater().inflate(R.layout.test, null);, R.layout.test是新更改的测试布局吗?
  • 不,所有资源都是静态的,编译后无法修改。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-02-27
  • 2013-11-01
  • 2017-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多