Как реализовать это настраиваемое всплывающее меню с помощью Material Design Android?
Я хочу реализовать пользовательское всплывающее меню, например Twitter в Android, например, с изображением и рисунком, но я не знаю Не знаю, для чего этот компонент использовался.
В веб-сайте Material Design, google present это решение. Поэтому я считаю, что для этого есть собственное решение.
![enter image description here]()
Я попытался с всплывающее меню, но я не могу найти, как настроить макет этого представления, например что.
Ответы
Ответ 1
вы можете использовать ListPopupWindow, отправив свой пользовательский адаптер, с помощью которого вы можете управлять макетом каждой строки ListPopupWindow
. Что касается нормального PopupWindow
, вам необходимо предоставить представление привязки, и дополнительно вы должны вызвать setContentWidth
в экземпляре ListPopupWindow
, который устанавливает ширину всплывающего окна по размеру его содержимого. Это небольшая цена, которую вы должны заплатить, но для небольшого набора данных это не большое дело. У меня есть этот метод утилиты для получения максимальной ширины строки:
public int measureContentWidth(ListAdapter adapter) {
int maxWidth = 0;
int count = adapter.getCount();
final int widthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
final int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
View itemView = null;
for (int i = 0; i < count; i++) {
itemView = adapter.getView(i, itemView, this);
itemView.measure(widthMeasureSpec, heightMeasureSpec);
maxWidth = Math.max(maxWidth, itemView.getMeasuredWidth());
}
return maxWidth;
}
Ответ 2
Там виджет называется PopupMenu
, который в основном представляет собой меню, привязанное к определенному виду. Один из недостатков заключается в том, что по умолчанию не отображаются значки.
Однако вы можете использовать отражение и вызвать setForceShowIcon
, чтобы их открыть. Код, который вам нужен:
-
Так как a PopupMenu
привязан к определенному виду, ваш элемент ActionBar
имеет атрибут actionLayout
. Этот макет (action_item.xml
) может быть таким же простым, как:
<Button
xmlns:android="http://schemas.android.com/apk/res/android"
style="?attr/actionButtonStyle"
android:layout_gravity="center"
android:text="Show popup"
android:textStyle="bold"
android:textSize="12sp"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
-
ActionBar
стиль меню, содержащий ваш элемент с указанным выше макетом
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/popup_item"
android:title="Show popup"
android:showAsAction="always"
android:actionLayout="@layout/action_item"/>
</menu>
-
Ваш popup_menu.xml
, макет, который вы надуваете для своего PopupMenu
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/item1"
android:title="Item1"
android:icon="@mipmap/ic_launcher"/>
</menu>
-
И, наконец, код для выполнения инфляции при нажатии элемента ActionBar
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_add_item:
PopupMenu popup = new PopupMenu(this, item.getActionView());
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.popup_menu, popup.getMenu());
// Use reflection to invoke setForceShowIcon
try {
Field[] fields = popup.getClass().getDeclaredFields();
for (Field field : fields) {
if ("mPopup".equals(field.getName())) {
field.setAccessible(true);
Object menuPopupHelper = field.get(popup);
Class<?> classPopupHelper = Class
.forName(menuPopupHelper.getClass().getName());
Method setForceIcons = classPopupHelper
.getMethod("setForceShowIcon", boolean.class);
setForceIcons.invoke(menuPopupHelper, true);
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
popup.show();
return true;
}
return super.onOptionsItemSelected(item);
}
Обратите внимание, что для получения этого многострочного текста в меню вам нужно будет использовать actionLayout
для ваших пунктов всплывающего меню.
Ответ 3
Используйте фрагмент списка всплывающих окон.
Хорошая вещь о фрагментах заключается в том, что вы можете легко анимировать их
(Если вы не понимаете фрагменты, я рекомендую прочитать первое Фрагмент Введение)
Если вам нужен полный контроль над всплывающим контентом, см. Диалоговый фрагмент
Ответ 4
У меня такая же проблема. Но, наконец, я нашел в своем собственном решении, что я делюсь с вами своим кодом. Надеюсь, это вам поможет.
popupWindowDogs = popupWindowDogs();
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// popupWindowDogs.showAsDropDown(v, -5, 0);
popupWindowDogs().showAtLocation(v, Gravity.CENTER, 0, 0);
}
});
// Detect touched area
detector = new SimpleGestureFilter(this, this);
}
public PopupWindow popupWindowDogs()
{
// initialize a pop up window type
PopupWindow popupWindow = new PopupWindow(this);
// the drop down list is a list view
final ListView listView = new ListView(this);
// set our adapter and pass our pop up window contents
listView.setAdapter(dogsAdapter(popUpContents));
// listView.setBackgroundColor(Color.DKGRAY);
listView.setBackgroundResource(R.drawable.ss4);
listView.setPadding(0, 0, 0, 10);
listView.setDivider(null);
try {
listView.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
View c = listView.getChildAt(0);
String cc = listView.getChildAt(0).toString();
int scrolly = -c.getTop() + listView.getFirstVisiblePosition() * c.getHeight();
/*
* Toast.makeText(getApplicationContext(), scrolly + "", Toast.LENGTH_SHORT)
* .show();
*/}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
int totalItemCount) {
}
});
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.toString() + "", Toast.LENGTH_SHORT)
.show();
}
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View v, int arg2,
long arg3) {
try {
// TODO Auto-generated method stub
Context mContext = v.getContext();
Swipetouch mainActivity = ((Swipetouch) mContext);
// add some animation when a list item was clicked
Animation fadeInAnimation = AnimationUtils.loadAnimation(v.getContext(),
android.R.anim.fade_in);
fadeInAnimation.setDuration(10);
v.startAnimation(fadeInAnimation);
// dismiss the pop up
mainActivity.popupWindowDogs.dismiss();
// get the text and set it as the button text
String val = (String) arg0.getItemAtPosition(arg2);
// Toast.makeText(mContext, val, Toast.LENGTH_SHORT).show();
if (val.equals("Signup Now")) {
Intent ii = new Intent(getApplicationContext(), Registration.class);
startActivity(ii);
stopService(new Intent(Swipetouch.this, MyService.class));
stopService(new Intent(Swipetouch.this, MyService.class));
} else if (val.equals("Login")) {
Intent ii = new Intent(getApplicationContext(), MyLoginActivity.class);
startActivity(ii);
stopService(new Intent(Swipetouch.this, MyService.class));
} else if (val.equals("Exit")) {
finish();
stopService(new Intent(Swipetouch.this, MyService.class));
} else if (val.equals("Friends")) {
Intent ii = new Intent(getApplicationContext(), MyLoginActivity.class);
startActivity(ii);
} else if (val.equals("Exit")) {
stopService(new Intent(Swipetouch.this, MyService.class));
finish();
}
} catch (Exception e) {
Toast.makeText(Swipetouch.this, e.toString(), Toast.LENGTH_SHORT).show();
}
}
});
// some other visual settings
popupWindow.setFocusable(true);
popupWindow.setWidth(250);
// popupWindow.setHeight(300);
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
// set the list view as pop up window content
// SET WALLPAPER IMAGE
/*
* popupWindow.setBackgroundDrawable(getWallpaper()); popupWindow.setHeight(300);
*/
// layout.setBackgroundResource(R.drawable.sshadow);
// layout.setBackgroundColor(Color.TRANSPARENT);
// popupWindow.setContentView(layout);
popupWindow.setBackgroundDrawable(new ColorDrawable(
android.graphics.Color.TRANSPARENT));
popupWindow.setContentView(listView);
return popupWindow;
}
Ответ 5
Этот код работает в моем приложении.
Попробуйте следующее: -
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".LocationDetailsActivity">
<item xmlns:tools="http://schemas.android.com/tools"
android:icon="@android:drawable/ic_menu_mapmode"
app:showAsAction="ifRoom"
android:title="@string/title_of_menu"
tools:context=".LocationDetailsActivity">
//the menu list with icon
<menu>
<item
android:id="@+id/action_map_type_normal"
android:orderInCategory="100"
android:icon="some_icon" //place your icon here
android:title="Vacation spots" />
<item
android:id="@+id/action_map_type_satellite"
android:orderInCategory="100"
android:icon="some_icon" //place your icon here
android:title="Friends and family" />
<item
android:id="@+id/action_map_type_hybrid"
android:orderInCategory="100"
android:icon="some_icon" //place your icon here
android:title="Restaurants" />
</menu>
</item>
Вы можете пройти учебник от разных поставщиков
http://developer.android.com/guide/topics/ui/actionbar.html
http://www.vogella.com/tutorials/AndroidActionBar/article.html
http://www.androidhive.info/2013/11/android-working-with-action-bar/
Все они имеют отличные примеры и исходный код, чтобы помочь вам
Надеюсь, это поможет вам:)
Ответ 6
try {
java.lang.reflect.Field[] fields = popup.getClass().getDeclaredFields();
for (java.lang.reflect.Field field : fields) {
if ("mPopup".equals(field.getName())) {
field.setAccessible(true);
Object menuPopupHelper = field.get(popup);
Class<?> classPopupHelper = Class
.forName(menuPopupHelper.getClass().getName());
Method setForceIcons = classPopupHelper
.getMethod("setForceShowIcon", boolean.class);
setForceIcons.invoke(menuPopupHelper, true);
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
Ответ 7
Простое решение. Добавьте это непосредственно перед вами .show().
try {
Field mFieldPopup=popupMenu.getClass().getDeclaredField("mPopup");
mFieldPopup.setAccessible(true);
MenuPopupHelper mPopup = (MenuPopupHelper) mFieldPopup.get(popupMenu);
mPopup.setForceShowIcon(true);
} catch (Exception e) {}