我有一个微调器,用作下拉菜单。当我按下微调器时,它应该显示两个图像。它显示两个图像,但不是正确的图像。它在下拉菜单上方显示两个按钮。
我尝试在下拉菜单的位置移动,以查看放置是否是问题所在。我已经尝试过初始化代码。我尝试将微调器放在onClick()方法中。
// Drop down menu
int[] sights = {R.drawable.reticle,R.drawable.reticle2};
@Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
mTextureView = view.findViewById(R.id.texture);
mButtonVideo = view.findViewById(R.id.video);
mButtonVideo.setOnClickListener(this);
Spinner spin = view.findViewById(R.id.dropdownmenu);
CustomAdapter customAdapter=new CustomAdapter(getActivity().getApplicationContext(),sights);
spin.setAdapter(customAdapter);
}
CustomAdapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
public class CustomAdapter extends BaseAdapter {
private Context context;
private int[] sights;
private LayoutInflater inflater;
public CustomAdapter(Context applicationContext, int[] sights) {
this.context = applicationContext;
this.sights = sights;
inflater = (LayoutInflater.from(applicationContext));
}
@Override
public int getCount() {
return sights.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view == null) {
view = inflater.inflate(R.layout.fragment_camera2_video,(ViewGroup) null);
}
ImageView icon = view.findViewById(R.id.imageView1);
icon.setImageResource(sights[i]);
return view;
}
Layout.xml
<RelativeLayout
<Spinner
android:id="@+id/dropdownmenu"
android:layout_width="51dp"
android:layout_height="44dp"
android:layout_below="@+id/video"
android:layout_alignStart="@+id/texture"
android:layout_alignEnd="@+id/video"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:layout_marginEnd="0dp"
android:spinnerMode="dropdown"
android:dropDownVerticalOffset="-15dp"/>
我需要在下拉菜单上显示两个图像,单击它们后,将imageView更改为屏幕上其他位置。下拉菜单显示的其他按钮在下拉菜单中没有任何功能,但是它们确实具有其他单独的功能。
答案 0 :(得分:0)
您需要使用初始化数组。
int[] sights=new int[]{1,2,3};