无法设置 Popup 的高度和宽度 Window
Can't set height and width of Popup Window
我为弹出窗口做了一个布局 window,当我点击按钮显示弹出窗口时 window 它没有正确显示弹出对话框,只能看到部分视图
这个
当我点击 3 个点时。
我希望我的弹出窗口像这样显示
当我点击 3 个点时。
这是我的代码。
LayoutInflater layoutInflater
= (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
final View popupView = layoutInflater.inflate(R.layout.custom_dialog, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT);
ImageButton gmail= (ImageButton) popupView.findViewById(R.id.gmail);
gmail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
popupWindow.dismiss();
}
});
popupWindow.showAsDropDown(view.findViewById(R.id.more));
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageButton
android:id="@+id/gmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
app:srcCompat="@drawable/gmail"
android:backgroundTint="#000000"
android:layout_weight="0.25"/>
<ImageButton
android:id="@+id/hike"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/hike"
android:backgroundTint="#000000"
android:scaleType="fitXY"
android:layout_weight="0.25"/>
<ImageButton
android:id="@+id/hangouts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#000000"
app:srcCompat="@drawable/hangouts"
android:scaleType="fitXY"
android:layout_weight=".25"/>
<ImageButton
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/message"
android:scaleType="fitXY"
android:backgroundTint="#000000"
android:layout_weight=".25"/>
</LinearLayout>
谁能告诉我我错在哪里?
提前致谢。
感谢大家的帮助
使用setContentView
PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
popupWindow .setContentView(popupView);
替换
app:srcCompat="@drawable/hike"
和 android:src="@drawable/hike"
我为弹出窗口做了一个布局 window,当我点击按钮显示弹出窗口时 window 它没有正确显示弹出对话框,只能看到部分视图 这个
当我点击 3 个点时。
我希望我的弹出窗口像这样显示
当我点击 3 个点时。
这是我的代码。
LayoutInflater layoutInflater
= (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
final View popupView = layoutInflater.inflate(R.layout.custom_dialog, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT);
ImageButton gmail= (ImageButton) popupView.findViewById(R.id.gmail);
gmail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
popupWindow.dismiss();
}
});
popupWindow.showAsDropDown(view.findViewById(R.id.more));
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageButton
android:id="@+id/gmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
app:srcCompat="@drawable/gmail"
android:backgroundTint="#000000"
android:layout_weight="0.25"/>
<ImageButton
android:id="@+id/hike"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/hike"
android:backgroundTint="#000000"
android:scaleType="fitXY"
android:layout_weight="0.25"/>
<ImageButton
android:id="@+id/hangouts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#000000"
app:srcCompat="@drawable/hangouts"
android:scaleType="fitXY"
android:layout_weight=".25"/>
<ImageButton
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/message"
android:scaleType="fitXY"
android:backgroundTint="#000000"
android:layout_weight=".25"/>
</LinearLayout>
谁能告诉我我错在哪里? 提前致谢。 感谢大家的帮助
使用setContentView
PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
popupWindow .setContentView(popupView);
替换
app:srcCompat="@drawable/hike"
和 android:src="@drawable/hike"