问题描述
单击按钮时,我想显示一些选项.我现在使用上下文菜单.但是,在当前的应用程序设计中,下面显示的上下文菜单不好.我想更改上下文菜单的样式或使用其他控件,或者用其他内容替换上下文菜单.我该怎么做才能使我的应用设计更美丽?有材料上下文菜单上有任何材料设计库吗?
推荐答案
您可以使用从API level 1开始的PopupWindow.
in PopupWindow您实际上可以使用XML布局文件设计一些东西. 您可以包含任何内容(TextView,button,imageView,...),例如: 弹出窗口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:padding="15dp" android:background="#89000000" android:orientation="vertical"> <Button android:id="@+id/idClose" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Close" android:layout_below="@+id/textView4" android:layout_alignRight="@+id/textView4" android:layout_alignEnd="@+id/textView4" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="PopUp Window Title" android:id="@+id/textView" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:textStyle="bold" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Item One / Option One" android:id="@+id/textView2" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" android:gravity="center" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Item Two / Option Two" android:id="@+id/textView3" android:layout_below="@+id/textView2" android:layout_centerHorizontal="true" android:layout_marginTop="5dp" android:gravity="center" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="..." android:id="@+id/textView4" android:layout_below="@+id/textView3" android:layout_centerHorizontal="true" android:gravity="center" android:layout_marginTop="5dp" /> </RelativeLayout>
主要活动XML:
<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" tools:context=".MainActivity"> <Button android:id="@+id/idOpen" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Open" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> </RelativeLayout>
您可以在活动中弹出它:
package com.example.popup; import android.app.Activity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.WindowManager; import android.widget.Button; import android.widget.PopupWindow; public class MainActivity extends Activity { Button open; LayoutInflater inflater; View popUpView; PopupWindow popupWindow; Button close; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); open = (Button)findViewById(R.id.idOpen); open.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { popupWindow.showAsDropDown(open); } }); inflater =(LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE); popUpView = inflater.inflate(R.layout.popup_window,null); popupWindow = new PopupWindow(popUpView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT); close = (Button)popUpView.findViewById(R.id.idClose); close.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { popupWindow.dismiss(); } }); } }
希望这会有所帮助,愉快的编码:)
问题描述
I want to show some options when clicking a button. I use context menu now. But the context menu showing below Android 3 is not good with the current app design. I want to either change the style of the context menu or use some other controls or replace the context menu with something else. What can I do to make my app design more beautiful? Is there any material design library with material context menu?
推荐答案
You can use the PopupWindow which starts from API level 1.
In PopupWindow you can actually design something with XML Layout file. You can include anything (TextView,Button,ImageView,...) Like : PopUp Window 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:padding="15dp" android:background="#89000000" android:orientation="vertical"> <Button android:id="@+id/idClose" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Close" android:layout_below="@+id/textView4" android:layout_alignRight="@+id/textView4" android:layout_alignEnd="@+id/textView4" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="PopUp Window Title" android:id="@+id/textView" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:textStyle="bold" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Item One / Option One" android:id="@+id/textView2" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" android:gravity="center" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Item Two / Option Two" android:id="@+id/textView3" android:layout_below="@+id/textView2" android:layout_centerHorizontal="true" android:layout_marginTop="5dp" android:gravity="center" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="..." android:id="@+id/textView4" android:layout_below="@+id/textView3" android:layout_centerHorizontal="true" android:gravity="center" android:layout_marginTop="5dp" /> </RelativeLayout>
Main Activity XML :
<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" tools:context=".MainActivity"> <Button android:id="@+id/idOpen" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Open" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> </RelativeLayout>
And you can Pop It Up in Activity like :
package com.example.popup; import android.app.Activity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.WindowManager; import android.widget.Button; import android.widget.PopupWindow; public class MainActivity extends Activity { Button open; LayoutInflater inflater; View popUpView; PopupWindow popupWindow; Button close; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); open = (Button)findViewById(R.id.idOpen); open.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { popupWindow.showAsDropDown(open); } }); inflater =(LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE); popUpView = inflater.inflate(R.layout.popup_window,null); popupWindow = new PopupWindow(popUpView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT); close = (Button)popUpView.findViewById(R.id.idClose); close.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { popupWindow.dismiss(); } }); } }
HOPE THIS WILL HELP, HAPPY CODING :)