我是安卓新手。我正在尝试在 android 上实现可滚动的弹出窗口。
如果我制作 ScrollView 在我看来它显示错误ScrollView can host only an direct child (Details)
这是我的代码
private PopupWindow pwindo;
private void initiatePopupWindow() {
try {
// We need to get the instance of the LayoutInflater
LayoutInflater inflater = (LayoutInflater) About.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.screen_popup,
(ViewGroup) findViewById(R.id.popup_element));
pwindo = new PopupWindow(layout,700,1000, true);
// pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
// pwindo.showAsDropDown(layout, 80, 80);
btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(cancel_button_click_listener);
} catch (Exception e) {
e.printStackTrace();
}
}
private OnClickListener cancel_button_click_listener = new OnClickListener() {
public void onClick(View v) {
pwindo.dismiss();
}
};
还有我的看法
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/popup_element">
<TextView/>
<TextView/>
<TextView/>
<ImageButton/>
<ImageButton/>
.
.
.
.
</ScrollView>
提前致谢。
最佳答案
在 ScrollView 中放置一个 LinearLayout 作为 you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects
布局应该是这样的
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/popup_element">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView/>
<TextView/>
<TextView/>
<ImageButton/>
<ImageButton/>
</LinearLayout>
</ScrollView>
将所有这些 View 放在 LinearLayout
中
并为弹出窗口提供更少的宽度和高度
pwindo = new PopupWindow(layout,400,400, true);
关于Android ScrollView 弹出窗口不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34605164/