我正在尝试制作一个Android应用程序,该应用程序在单击按钮时会打开一个弹出窗口。并且,弹出窗口具有四个带标题的TextView和四个带选项的listView。但是,我无法在弹出窗口中添加listView。单击按钮时会弹出窗口,但是当我添加listView及其适配器并尝试运行它时,应用程序崩溃。我希望PopUp窗口的布局与news.xml文件中的布局一样。
activity_main.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:visibility="visible"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/linear">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/news1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginStart="15dp"
android:text="@string/news" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
MainActivity.java
package com.example.abina.popup;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import static android.view.Gravity.NO_GRAVITY;
public class MainActivity extends AppCompatActivity {
private Button news1;
private PopupWindow popupWindow;
private LayoutInflater layoutInflater;
private LinearLayout linearLayout;
ListView lst;
String [] local = {"asdf","Sgadf","adfhtr","trdbfa"};
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//for listView 1
lst = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,local);
lst.setAdapter(adapter);
lst.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(MainActivity.this,local[i],Toast.LENGTH_SHORT).show();
}
});
linearLayout = (LinearLayout) findViewById(R.id.linear);
news1 = (Button) findViewById(R.id.news1);
news1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
layoutInflater = (LayoutInflater) getApplication().getSystemService(LAYOUT_INFLATER_SERVICE);
ViewGroup container = (ViewGroup) layoutInflater.inflate(R.layout.news,null);
popupWindow = new PopupWindow(container,800,1300,true);
popupWindow.showAsDropDown(news1);
container.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
popupWindow.dismiss();
return false;
}
});
}
});
}
}
news.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ABEBC6">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true">
<ListView
android:id="@+id/listView1"
android:layout_width="160dp"
android:layout_height="184dp"
android:layout_alignParentStart="true"
android:layout_below="@+id/textView"
android:layout_marginStart="13dp" />
<TextView
android:id="@+id/textView"
android:layout_width="109dp"
android:layout_height="33dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="30dp"
android:layout_marginTop="20dp"
android:text="@string/local" />
<TextView
android:id="@+id/textView2"
android:layout_width="108dp"
android:layout_height="33dp"
android:layout_alignParentEnd="true"
android:layout_alignTop="@+id/textView"
android:layout_marginEnd="30dp"
android:text="@string/national" />
<ListView
android:id="@+id/listView2"
android:layout_width="160dp"
android:layout_height="184dp"
android:layout_alignParentEnd="true"
android:layout_alignTop="@+id/listView1" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="260dp">
<TextView
android:id="@+id/textView3"
android:layout_width="109dp"
android:layout_height="33dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="30dp"
android:layout_marginTop="20dp"
android:text="@string/sports1" />
<TextView
android:id="@+id/textView4"
android:layout_width="108dp"
android:layout_height="33dp"
android:layout_alignParentEnd="true"
android:layout_alignTop="@+id/textView3"
android:layout_marginEnd="43dp"
android:text="@string/sports2" />
<ListView
android:id="@+id/listView3"
android:layout_width="160dp"
android:layout_height="184dp"
android:layout_alignParentStart="true"
android:layout_below="@+id/textView3"
android:layout_marginStart="20dp" />
<ListView
android:id="@+id/listView4"
android:layout_width="162dp"
android:layout_height="184dp"
android:layout_alignParentEnd="true"
android:layout_below="@+id/textView4" />
</RelativeLayout>
</RelativeLayout>
strings.xml
<resources>
<string name="app_name">Popup</string>
<string name="news">Check Latest News!</string>
<string name="local">Local News</string>
<string name="national">National News</string>
<string name="sports1">Sports One</string>
<string name="sports2">Sports Two</string>
<string-array name="local">
<item>Star Ledger</item>
<item>Ny Times</item>
<item>The Record</item>
<item>Trentorian</item>
</string-array>
</resources>
答案 0 :(得分:0)
在您的动作监听器中添加以下内容:
news1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PopupWindow popupWindow = new PopupWindow(getApplicationContext());
ArrayList<String> sortList = new ArrayList<>();
sortList.add("A to Z");
sortList.add("Z to A");
sortList.add("Any");
sortList.add("Test");
ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_dropdown_item_1line,
sortList);
ListView listViewSort = new ListView(getApplicationContext());
listViewSort.setAdapter(adapter);
// set on item selected
listViewSort.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getApplicationContext(),
"You select in popup menu" + adapterView.getAdapter().getItem(i).toString(), Toast.LENGTH_LONG).show();
popupWindow.dismiss();
}
});
popupWindow.setFocusable(true);
popupWindow.setWidth(WindowManager.LayoutParams.FILL_PARENT);
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setContentView(listViewSort);
popupWindow.showAsDropDown(view, 0, 0);
});
}
答案 1 :(得分:0)
问题在于您的列表在activity_main布局中不存在。而是存在于弹出窗口中。
将您的onCreate()方法更改为下面的代码,它应该可以正常工作。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//for listView 1
linearLayout = (LinearLayout) findViewById(R.id.linear);
news1 = (Button) findViewById(R.id.news1);
news1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
layoutInflater = (LayoutInflater) getApplication().getSystemService(LAYOUT_INFLATER_SERVICE);
ViewGroup container = (ViewGroup) layoutInflater.inflate(R.layout.news,null);
popupWindow = new PopupWindow(container,800,1300,true);
lst = container.findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,local);
lst.setAdapter(adapter);
lst.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(MainActivity.this,local[i],Toast.LENGTH_SHORT).show();
}
});
popupWindow.showAsDropDown(news1);
container.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
popupWindow.dismiss();
return false;
}
});
}
});
}
希望这对您有所帮助。 最好的问候,
答案 2 :(得分:0)
以下是带有两个列表视图和文本视图的弹出菜单示例:
PopupWindow popupWindow = new PopupWindow(getApplicationContext());
LayoutInflater inflater = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup_element,
(ViewGroup) view, false);
//List first
TextView nameOfList1 = layout.findViewById(R.id.text1);
nameOfList1.setText("List one:");
ListView mListView1 = layout.findViewById(R.id.listView1);
final String[] data1 = {"Value from list 1", "Value from list 2", "Value from list 3", "Value from list 4",
"Value from list 5", "Value from list 6"};
mListView1.setAdapter(new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, data1));
mListView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getApplicationContext(), "You select value from list one " + data1[i], Toast.LENGTH_LONG).show();
}
});
//List second
TextView nameOfList2 = layout.findViewById(R.id.text2);
nameOfList2.setText("Select planet from list");
ListView mListView2 = layout.findViewById(R.id.listView2);
final String[] data2 = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"};
mListView2.setAdapter(new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, data2));
mListView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getApplicationContext(), "You select plane " + data2[i], Toast.LENGTH_LONG).show();
}
});
popupWindow.setFocusable(true);
popupWindow.setWidth(WindowManager.LayoutParams.FILL_PARENT);
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setContentView(layout);
popupWindow.showAsDropDown(view, 0, 0);
和popup_element.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollojt"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#f00"></ListView>
<TextView
android:id="@+id/text2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<ListView
android:id="@+id/listView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/listView1"
android:background="#0f0"></ListView>
</LinearLayout>
</ScrollView>