Android如何控制PopupMenu弹出菜单的宽度尺寸
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android如何控制PopupMenu弹出菜单的宽度尺寸相关的知识,希望对你有一定的参考价值。
本身是模拟微信的菜单,但是无论如何都无法更改PopupMenu弹出的菜单的尺寸。
我想要的效果描述
当点击【找产品】弹出的菜单要和找产品的宽度一样宽,而不是占据大半个屏幕。
我的代码:
android 中尺寸的设置几种形式,可以是wrap_content/fill_parent/还可以是具体的数值,不如13dp。 参考技术A 在那个R.menu.menuns1.xml里面设置吧,你想要多大的尺寸就把这个布局设置成多大!追问
如何设置,给个例子?
追答我这边是使用的PopWindow, pWindow = new PopupWindow(v, 120,LayoutParams.WRAP_CONTENT);用这个可以设置尺寸!
追问可以把你这个PopupWindow实例的代码单独发送给我吗?二五三二五九二八三七,非常感谢!
追答发过去了
本回答被提问者和网友采纳android中RecyclerView控件实现长按弹出PopupMenu菜单功能
之前写过一篇文章:android中实现简单的聊天功能
现在是在之前功能的基础上,添加一个长按聊天记录,删除对应聊天记录的功能
RecyclerView控件,没有对应的长按事件,我们需要自己手工添加,修改对应的适配器类,注意红色内容为添加内容
ChatAdapter.java:
package com.example.chenrui.app1; import android.support.annotation.NonNull; import android.support.v7.widget.RecyclerView; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.TextView; import com.example.chenrui.common.Chat; import java.util.List; public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.ViewHolder> { private List<Chat> chatList; public ChatAdapter(List<Chat> chatList) { this.chatList = chatList; } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.chat_item,viewGroup,false); ViewHolder viewHolder = new ViewHolder(view); return viewHolder; } @Override public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) { Chat chat = chatList.get(i); viewHolder.chatText.setText(chat.getContent()); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(viewHolder.chatText.getLayoutParams()); if(chat.getTargetUser().equals("张三")) { params.gravity = Gravity.LEFT; } else { params.gravity = Gravity.RIGHT; } viewHolder.chatText.setLayoutParams(params); if(onItemClickListener!=null) { viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { onItemClickListener.onItemLongClick(viewHolder.itemView,i); return false; } }); } } @Override public int getItemCount() { return chatList.size(); } static class ViewHolder extends RecyclerView.ViewHolder { TextView chatText; public ViewHolder(@NonNull View itemView) { super(itemView); chatText = itemView.findViewById(R.id.chatText); } } private OnItemClickListener onItemClickListener; public interface OnItemClickListener{ void onItemLongClick(View view , int pos); } public void setOnItemClickListener(OnItemClickListener onItemClickListener) { this.onItemClickListener = onItemClickListener; } }
修改对应的类,注意红色内容为添加内容
这里实现了长按聊天内容,弹出一个PopupMenu菜单,PopupMenu菜单的使用参考我的上一篇文章:Android中创建PopupMenu弹出式菜单
菜单有一个删除功能,点击删除功能,删除对应的聊天记录
MainActivity.java
package com.example.chenrui.app1; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.MenuItem; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.PopupMenu; import android.widget.Spinner; import com.example.chenrui.common.Chat; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final RecyclerView chatList = findViewById(R.id.chatList); final List<Chat> chatData = new ArrayList<Chat>(); final ChatAdapter chatAdapter = new ChatAdapter(chatData); chatList.setAdapter(chatAdapter); LinearLayoutManager layoutManager = new LinearLayoutManager(this); chatList.setLayoutManager(layoutManager);
chatAdapter.setOnItemClickListener(new ChatAdapter.OnItemClickListener() { @Override public void onItemLongClick(View view, final int pos) { PopupMenu popupMenu = new PopupMenu(MainActivity.this,view); popupMenu.getMenuInflater().inflate(R.menu.menu3,popupMenu.getMenu()); //弹出式菜单的菜单项点击事件 popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { if(item.getItemId()==R.id.delete) { chatData.remove(pos); chatAdapter.notifyItemRemoved(pos); } return false; } }); popupMenu.show(); } }); final Spinner targetUser = findViewById(R.id.targetUser); String[] data = {"张三","李四"}; ArrayAdapter<String> adapter = new ArrayAdapter<String> (this,R.layout.support_simple_spinner_dropdown_item,data); targetUser.setAdapter(adapter); final EditText chatContent = findViewById(R.id.chatContent); Button chatSubmit = findViewById(R.id.chatSubmit); chatSubmit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(!chatContent.getText().toString().equals("")) { String user = targetUser.getSelectedItem().toString(); chatData.add(new Chat(user, chatContent.getText().toString())); chatAdapter.notifyItemInserted(chatData.size() - 1); chatList.scrollToPosition(chatData.size() - 1); chatContent.setText(""); } } }); } }
显示效果:
以上是关于Android如何控制PopupMenu弹出菜单的宽度尺寸的主要内容,如果未能解决你的问题,请参考以下文章
android中RecyclerView控件实现长按弹出PopupMenu菜单功能