Android 将弹出菜单设置为全屏
注意:这是弹出式菜单,不是弹出式窗口。所以请大家仔细阅读 我已经实现了弹出菜单。它显示在屏幕的一半。我想把它扩展到整个设备的宽度。我试图通过将Android 将弹出菜单设置为全屏,android,popupmenu,Android,Popupmenu,注意:这是弹出式菜单,不是弹出式窗口。所以请大家仔细阅读 我已经实现了弹出菜单。它显示在屏幕的一半。我想把它扩展到整个设备的宽度。我试图通过将layout\u width设置为match\u parent来改变它的样式,但没有成功 以下是我迄今为止所做的尝试: 风格 <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"&g
layout\u width
设置为match\u parent
来改变它的样式,但没有成功
以下是我迄今为止所做的尝试:
风格
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="popupMenuStyle">@style/PopupMenu</item>
</style>
<!-- Change Overflow Menu Background -->
<style name="PopupMenu" parent="android:Widget.Holo.Light.ListPopupWindow">
<item name="android:popupBackground">#888888</item>
<item name="android:layout_width">match_parent</item>
</style>
@样式/弹出菜单
#888888
匹配父项
下面是我的java代码:
PopupMenu menu = new PopupMenu(getActivity(), tvnext);
for (int i = 0; i < array.size(); i++) {
menu.getMenu().add(1, i, 1, array.get(i).getAccountName());
}
menu.show();
menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
setUpNextFunctionality(item.getItemId());
return false;
}
});
PopupMenu=new PopupMenu(getActivity(),tvnext);
对于(int i=0;i
请不要建议我使用弹出窗口。如果不起作用,这是我的最后一个选择。尝试以下操作:
pwindow =
new PopupWindow(layoutt,LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,true);
试试这个
充气机。充气(右布局。菜单,菜单)
或
菜单。充气(右布局。弹出菜单) 如果不实现类似于
PopupMenu
的弹出窗口,我认为您无法做到这一点。如果要选中菜单OpupHelper::createPopup:
@NonNull
private MenuPopup createPopup() {
final WindowManager windowManager = (WindowManager) mContext.getSystemService(
Context.WINDOW_SERVICE);
final Display display = windowManager.getDefaultDisplay();
final Point displaySize = new Point();
if (Build.VERSION.SDK_INT >= 17) {
display.getRealSize(displaySize);
} else if (Build.VERSION.SDK_INT >= 13) {
display.getSize(displaySize);
} else {
displaySize.set(display.getWidth(), display.getHeight());
}
final int smallestWidth = Math.min(displaySize.x, displaySize.y);
final int minSmallestWidthCascading = mContext.getResources().getDimensionPixelSize(
R.dimen.abc_cascading_menus_min_smallest_width);
final boolean enableCascadingSubmenus = smallestWidth >= minSmallestWidthCascading;
final MenuPopup popup;
if (enableCascadingSubmenus) {
popup = new CascadingMenuPopup(mContext, mAnchorView, mPopupStyleAttr,
mPopupStyleRes, mOverflowOnly);
} else {
popup = new StandardMenuPopup(mContext, mMenu, mAnchorView, mPopupStyleAttr,
mPopupStyleRes, mOverflowOnly);
}
// Assign immutable properties.
popup.addMenu(mMenu);
popup.setOnDismissListener(mInternalOnDismissListener);
// Assign mutable properties. These may be reassigned later.
popup.setAnchorView(mAnchorView);
popup.setCallback(mPresenterCallback);
popup.setForceShowIcon(mForceShowIcon);
popup.setGravity(mDropDownGravity);
return popup;
}
您将看到弹出菜单的大小是根据显示大小硬编码的。所以,最简单的方法可能是检查
PopupMenu
相关的源代码并实现类似的功能,但大小要符合您的需要。尝试在您的样式中设置minWidth,如下所示
<item name="android:minWidth">1000dp</item>
1000dp
我知道你的问题了。不要修改某些东西,而是使用新的小部件,它可以轻松满足您的需要,并与新版本兼容
谷歌推出新材料概念
要在android中使用它,您可以使用git hub库,如 使其半透明。我做了同样的弹出像这样。请检查一下这个。也许它对你也有用。 在您的清单中:
<activity
android:name=".ActivityInviteFriend"
android:theme="@style/Theme.TransparentInfo">
</activity>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/LightappActionBarColor</item>
<item name="colorPrimaryDark">@color/appColor</item>
<item name="colorAccent">@color/btn_color</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
<color name="semiTransparentBlack">#00000000</color>
<style name="Theme.TransparentInfo" parent="AppTheme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@color/semiTransparentBlack</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
package com.gc.naifizzy;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;
public class ActivityInviteFriend extends AppCompatActivity {
Button btn_send;
Snackbar snackbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
setContentView(R.layout.activity_invite_friend);
btn_send = (Button) findViewById(R.id.btn_send);
btn_send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//snack("Invitation sent successfully...");
Toast.makeText(ActivityInviteFriend.this, "Invitation sent successfully", Toast.LENGTH_SHORT).show();
finish();
}
});
}
public void snack(String data) {
snackbar = Snackbar
.make(findViewById(android.R.id.content), data, Snackbar.LENGTH_LONG)
.setAction("Dismiss", new View.OnClickListener() {
@Override
public void onClick(View view) {
snackbar.dismiss();
}
});
snackbar.show();
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="#502f2f2f">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_centerInParent="true"
android:background="@android:color/white"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<RelativeLayout
android:id="@+id/dds"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#112f2f2f"
android:paddingTop="10dp"
android:visibility="visible">
<TextView
android:id="@+id/txt_what"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:gravity="center_vertical|left"
android:paddingLeft="10dp"
android:text="Invite people to join your tree on Naifizzy"
android:textColor="@color/appColor"
android:textSize="15sp"
android:textStyle="bold" />
</RelativeLayout>
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_below="@id/dds"
android:layout_marginTop="3dp"
android:alpha="0.5"
android:background="@color/appColor"
android:visibility="visible" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/view"
android:layout_marginTop="20dp"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical">
<TextView
android:id="@+id/txt_share"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="Invite people by sharing this link "
android:textColor="#992f2f2f"
android:textSize="15sp" />
<EditText
android:id="@+id/edt_user_link"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@id/txt_share"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/layout_border"
android:editable="false"
android:paddingLeft="10dp"
android:singleLine="true"
android:text="http://naifizzy.com/@parik_dhakan"
android:textColor="#992f2f2f"
android:textSize="15sp" />
<View
android:id="@+id/view1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@id/edt_user_link"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:alpha="0.5"
android:background="@color/appColor" />
<TextView
android:id="@+id/txt_share2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/view1"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="Send by email "
android:textColor="#992f2f2f"
android:textSize="15sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginBottom="30dp"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:visibility="visible"
android:weightSum="1">
<LinearLayout
android:id="@+id/ed_l"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:background="@drawable/layout_border"
android:orientation="horizontal"
android:visibility="visible">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TextInputLayout
android:id="@+id/edl_current"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:paddingTop="5dp"
android:textColorHint="#992f2f2f"
android:textSize="15sp">
<EditText
android:id="@+id/edt_mail"
style="@style/StyledTilEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:background="@android:color/transparent"
android:hint="Enter recipients' Email seprated by commas "
android:inputType="textEmailAddress"
android:paddingLeft="10dp"
android:textColor="#992f2f2f"
android:textColorHint="#992f2f2f"
android:textSize="15sp" />
</android.support.design.widget.TextInputLayout>
<ImageView
android:id="@+id/img_show"
android:layout_width="30dp"
android:layout_height="25dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="20dp"
android:scaleType="centerInside"
android:src="@drawable/ic_eye"
android:visibility="gone" />
</RelativeLayout>
</LinearLayout>
<EditText
android:id="@+id/edt_user_link2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/txt_share2"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/layout_border"
android:paddingLeft="10dp"
android:singleLine="true"
android:text="http://naifizzy.com/@parik_dhakan"
android:textColor="#992f2f2f"
android:textSize="15sp"
android:visibility="gone" />
<Button
android:id="@+id/btn_send"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:layout_weight="0.8"
android:background="@drawable/signupbg"
android:gravity="center_vertical|center_horizontal"
android:text="Send\nInvites"
android:textColor="@android:color/white"
android:textSize="10sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
最后是您的xml布局:
<activity
android:name=".ActivityInviteFriend"
android:theme="@style/Theme.TransparentInfo">
</activity>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/LightappActionBarColor</item>
<item name="colorPrimaryDark">@color/appColor</item>
<item name="colorAccent">@color/btn_color</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
<color name="semiTransparentBlack">#00000000</color>
<style name="Theme.TransparentInfo" parent="AppTheme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@color/semiTransparentBlack</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
package com.gc.naifizzy;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;
public class ActivityInviteFriend extends AppCompatActivity {
Button btn_send;
Snackbar snackbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
setContentView(R.layout.activity_invite_friend);
btn_send = (Button) findViewById(R.id.btn_send);
btn_send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//snack("Invitation sent successfully...");
Toast.makeText(ActivityInviteFriend.this, "Invitation sent successfully", Toast.LENGTH_SHORT).show();
finish();
}
});
}
public void snack(String data) {
snackbar = Snackbar
.make(findViewById(android.R.id.content), data, Snackbar.LENGTH_LONG)
.setAction("Dismiss", new View.OnClickListener() {
@Override
public void onClick(View view) {
snackbar.dismiss();
}
});
snackbar.show();
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="#502f2f2f">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_centerInParent="true"
android:background="@android:color/white"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<RelativeLayout
android:id="@+id/dds"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#112f2f2f"
android:paddingTop="10dp"
android:visibility="visible">
<TextView
android:id="@+id/txt_what"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:gravity="center_vertical|left"
android:paddingLeft="10dp"
android:text="Invite people to join your tree on Naifizzy"
android:textColor="@color/appColor"
android:textSize="15sp"
android:textStyle="bold" />
</RelativeLayout>
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_below="@id/dds"
android:layout_marginTop="3dp"
android:alpha="0.5"
android:background="@color/appColor"
android:visibility="visible" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/view"
android:layout_marginTop="20dp"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical">
<TextView
android:id="@+id/txt_share"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="Invite people by sharing this link "
android:textColor="#992f2f2f"
android:textSize="15sp" />
<EditText
android:id="@+id/edt_user_link"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@id/txt_share"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/layout_border"
android:editable="false"
android:paddingLeft="10dp"
android:singleLine="true"
android:text="http://naifizzy.com/@parik_dhakan"
android:textColor="#992f2f2f"
android:textSize="15sp" />
<View
android:id="@+id/view1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@id/edt_user_link"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:alpha="0.5"
android:background="@color/appColor" />
<TextView
android:id="@+id/txt_share2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/view1"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="Send by email "
android:textColor="#992f2f2f"
android:textSize="15sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginBottom="30dp"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:visibility="visible"
android:weightSum="1">
<LinearLayout
android:id="@+id/ed_l"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:background="@drawable/layout_border"
android:orientation="horizontal"
android:visibility="visible">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TextInputLayout
android:id="@+id/edl_current"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:paddingTop="5dp"
android:textColorHint="#992f2f2f"
android:textSize="15sp">
<EditText
android:id="@+id/edt_mail"
style="@style/StyledTilEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:background="@android:color/transparent"
android:hint="Enter recipients' Email seprated by commas "
android:inputType="textEmailAddress"
android:paddingLeft="10dp"
android:textColor="#992f2f2f"
android:textColorHint="#992f2f2f"
android:textSize="15sp" />
</android.support.design.widget.TextInputLayout>
<ImageView
android:id="@+id/img_show"
android:layout_width="30dp"
android:layout_height="25dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="20dp"
android:scaleType="centerInside"
android:src="@drawable/ic_eye"
android:visibility="gone" />
</RelativeLayout>
</LinearLayout>
<EditText
android:id="@+id/edt_user_link2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/txt_share2"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/layout_border"
android:paddingLeft="10dp"
android:singleLine="true"
android:text="http://naifizzy.com/@parik_dhakan"
android:textColor="#992f2f2f"
android:textSize="15sp"
android:visibility="gone" />
<Button
android:id="@+id/btn_send"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:layout_weight="0.8"
android:background="@drawable/signupbg"
android:gravity="center_vertical|center_horizontal"
android:text="Send\nInvites"
android:textColor="@android:color/white"
android:textSize="10sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
您将在下面的链接中找到答案,请尝试阅读我的问题。它是弹出菜单,而不是窗口。我特别提到问题末尾的注释。好问题。。。。。。根据我的说法,这是不可能的。。。。在我的项目中尝试你想要的,但未能获得成功。。。。。我正在检查更多关于您的问题。。。。。。。如果你获得了成功,那么你的答案对我来说很有趣。。。。如果有人根据你的要求回答这个问题,我会很感激……我正在做的最糟糕的事情是改变宽度。。。。试试这个,在字符串末尾的标题上加上更多的空格,就像android:title=“Edit Profile.”/>我尝试过这个想法,但我有来自web服务的数据。因此,这不是有效的解决方案。我们必须对此进行适当的破解。试着阅读我的问题。它是弹出菜单,而不是窗口。我在问题的最后特别提到了这张便条。欢迎来到苏拉维纳。但不幸的是,这是用于膨胀菜单(从R菜单)。所以这并不能帮助我们设置自定义布局。我如何在片段类中使用这个方法。我以前在课堂上复制,但无法导入菜单。你的方法在某种程度上是正确的。但是创建另一个活动会有自己的生命周期,对于我的情况,我希望在另一个线程中创建一个弹出窗口。它没有效果。