我正在制作一个Android应用程序,并且在某些活动中,当单击某些项目时,我正在使该应用程序显示PopupMenu。
问题在于PopupMenu不能一次显示所有选项,相反,我必须向下滚动PopupMenu才能查看其余菜单选项。
这个问题发生在两个活动中,但是在第三个活动中,它依我的意愿显示,尽管我在这三个活动中都使用相同的代码来创建PopupMenu!
这两张照片澄清了我遇到的问题: enter image description here enter image description here
,这是该活动的照片,按预期显示了菜单: enter image description here
这是两个有问题的活动之一的代码:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupMenu;
import com.example.android.grad_qurantutor.data.DatabaseHelper;
import java.util.ArrayList;
public class FavoritesActivity extends AppCompatActivity {
private DatabaseHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.general_list);
dbHelper = new DatabaseHelper(this);
final ArrayList<Ayah> arr = dbHelper.getFavorites();
if(arr.size() > 0) {
final AyahAdapter itemsAdapter = new AyahAdapter(this, arr);
final ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(itemsAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
final Ayah a = arr.get(i);
PopupMenu popup = new PopupMenu(FavoritesActivity.this, listView);
popup.getMenuInflater().inflate(R.menu.menu_favorites_options, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
// Respond to a click on the "Add to Favorites" menu option
case R.id.unfavorite:
dbHelper.unfavorite(a.getId());
ArrayList<Ayah> arr = dbHelper.getFavorites();
itemsAdapter.clear();
itemsAdapter.addAll(arr);
itemsAdapter.notifyDataSetChanged();
return true;
case R.id.go_to_surah:
Surah surah = dbHelper.getSurah(a.getSurahId());
Intent myIntent = new Intent(FavoritesActivity.this, AyahsActivity.class);
myIntent.putExtra("surahObj", surah);
myIntent.putExtra("ayahNum", a.getNumber());
startActivity(myIntent);
return true;
case R.id.share_ayah:
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
String shareBody = a.getText();
shareIntent.putExtra(Intent.EXTRA_TEXT , shareBody);
startActivity(Intent.createChooser(shareIntent , "Share Using"));
return true;
}
return true;
}
});
popup.show();
}
});
}
}
}
AND ,这是菜单xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/unfavorite"
android:title="Unfavorite" />
<item
android:id="@+id/go_to_surah"
android:title="Go to the Surah" />
<item
android:id="@+id/share_ayah"
android:title="Share" />
</menu>
AND 这是活动的xml布局:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFF8E1"
android:drawSelectorOnTop="true"
android:orientation="vertical" />
以及以下是正确显示菜单的活动代码:
活动:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupMenu;
import com.example.android.grad_qurantutor.data.DatabaseHelper;
import java.util.ArrayList;
public class SearchActivity extends AppCompatActivity {
private DatabaseHelper dbHelper;
class Listener implements AdapterView.OnItemClickListener{
private ArrayList<Ayah> arr;
private ListView listView;
public Listener(ArrayList<Ayah> arr, ListView listView){
this.arr = arr;
this.listView = listView;
}
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
final Ayah a = arr.get(i);
PopupMenu popup = new PopupMenu(SearchActivity.this, listView);
popup.getMenuInflater().inflate(R.menu.menu_searched_ayahs_options, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
// Respond to a click on the "Add to Favorites" menu option
case R.id.add_favorite2:
dbHelper.addFavoriteAyah(a.getId());
return true;
case R.id.go_to_surah2:
Surah surah = dbHelper.getSurah(a.getSurahId());
Intent myIntent = new Intent(SearchActivity.this, AyahsActivity.class);
myIntent.putExtra("surahObj", surah);
myIntent.putExtra("ayahNum", a.getNumber());
startActivity(myIntent);
return true;
case R.id.share_ayah:
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
String shareBody = a.getText();
shareIntent.putExtra(Intent.EXTRA_TEXT , shareBody);
startActivity(Intent.createChooser(shareIntent , "Share Using"));
return true;
}
return true;
}
});
popup.show();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
}
public void search(View view){
dbHelper = new DatabaseHelper(this);
EditText editText = (EditText) findViewById(R.id.search_edit_text);
ArrayList<Ayah> arr = dbHelper.search(editText.getText().toString().trim());
if(arr.size() > 0) {
AyahAdapter itemsAdapter = new AyahAdapter(this, arr);
ListView listView = (ListView) findViewById(R.id.search_list);
listView.setAdapter(itemsAdapter);
listView.setOnItemClickListener(new Listener(arr, listView));
}
}
}
AND ,这是菜单xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/add_favorite2"
android:title="Add to Favorites" />
<item
android:id="@+id/go_to_surah2"
android:title="Go to the Surah" />
<item
android:id="@+id/share_ayah"
android:title="Share" />
</menu>
AND ,这是活动的xml布局:
<LinearLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFF8E1"
android:orientation="vertical"
tools:context=".SearchActivity">
<EditText
android:id="@+id/search_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginTop="24dp"
android:layout_marginRight="24dp"
android:layout_marginBottom="12dp"
android:hint="search" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="24dp"
android:onClick="search"
android:text="Search" />
<ListView
android:id="@+id/search_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#FFF8E1"
android:drawSelectorOnTop="true"
android:orientation="vertical" />
</LinearLayout>
答案 0 :(得分:0)
您将要使用popup.setHeight()
设置所需的高度。
您可能还想使用popup.showAsDropdown()
来更好地控制弹出窗口的显示位置。例如,我希望我的位置相对于enterPhoneNumberButton
出现在某个位置:
int xOffset = popupWidthInDP > enterPhoneNumberButton.getWidth() ? 0 : enterPhoneNumberButton.getWidth() / 2 - dpToPx(popupWidthInDP / 2);
popup.showAsDropDown(enterPhoneNumberButton, xOffset, -enterPhoneNumberButton.getHeight() - dpToPx(popupHeightInDP));