gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:22:50 24 4
gpt4 key购买 nike

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

测试.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;
}
});
}
});
}
}

最佳答案

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

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

关于java - 如何从不同的 XML 更改 ImageView 源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40048392/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com