나는 PopupMenu를 사용하도록 추천 할 것입니다. 사용하기 쉽고 멋지게 보입니다. 여기
내가 그것을 사용하는 방법 예는 다음과 같습니다
활동
View view = findViewById(R.id.action_settings);
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final ListView listView = (ListView) popupView.findViewById(R.id.listView);
String[] functions = {getString(R.string.shareScreenshot), getString(R.string.shareDatei), getString(R.string.shareXML)};
ListAdapter adapter = new CustomPopupAdapter(this, functions, listView);
listView.setAdapter(adapter);
Display display = (this.getWindowManager().getDefaultDisplay());
Point size = new Point();
display.getSize(size);
int width = size.x;
//int height = size.y;
Resources resources = this.getResources();
int navigationBarHeight = 0;
int statusbarHeight = 0;
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
navigationBarHeight = resources.getDimensionPixelSize(resourceId);
}
resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
statusbarHeight = resources.getDimensionPixelSize(resourceId);
}
final PopupWindow popupWindow = new PopupWindow(this);
popupWindow.setContentView(popupView);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
popupWindow.setBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.shadow_02327));
} else {
popupWindow.setBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.shadow_02327));
}
popupWindow.setWidth(width);
popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, 0, navigationBarHeight + statusbarHeight);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Click handle
}
});
}
어댑터
public class CustomPopupAdapter extends ArrayAdapter {
private String[] option;
ListView owner;
public CustomPopupAdapter(Context context, String[] option, ListView owner) {
super(context, R.layout.custom_row_settings, option);
this.option = option;
this.owner = owner;
}
@Override
public View getView(int pos, View view, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View customView = inflater.inflate(R.layout.popup_row_image_text, parent, false);
ImageView iv = (ImageView) customView.findViewById(R.id.imageView);
TextView tv = (TextView) customView.findViewById(R.id.textView);
tv.setText(option[pos]);
switch (pos) {
case 0:
iv.setImageResource(R.drawable.ic_photo_camera_grey_24dp);
break;
case 1:
iv.setImageResource(R.drawable.ic_insert_drive_file_grey_24dp);
break;
case 2:
iv.setImageResource(R.drawable.ic_code_grey_24dp);
break;
case 3:
iv.setImageResource(R.drawable.ic_move_to_inbox_grey_24dp);
break;
}
return customView;
}
}
popup.xml을
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:background="@color/white">
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</RelativeLayout>
은 어떻게 마지막 탭 팝업 메뉴에서 선택한 옵션을 가져오고 선택한 탭으로 보일 수 있는가? –
예제의 앵커가 null입니다. 원하는 탭으로 변경하십시오! 그럼 당신은 그것을 가지고 있어야합니다 :) – XxGoliathusxX
이것은 내 질문에 대답하지 않는다, 나는 드롭 다운으로 탭을 설정하는 방법을 알고 싶다, 나는 팝업 메뉴를 만드는 방법을 알고있다. 그러나 당신이 탭에서 팝업 메뉴를 사용하도록 제안한다면 탭과 관련된 코드로 어떻게 설명하십시오. –