如何自定义此图片中的菜单。任何人都可以告诉解决方案
我想显示四个子菜单(FB,Google +,Twitter和SeeAll)如果我触摸看到一个对话框必须弹出,其中包含更多(FB,Google +,Twitter,Linkedin,NetLog等) ..,)
代码:
menu.xml文件: -
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/menu_item_share"
android:showAsAction="ifRoom"
android:title="Share"
android:actionProviderClass="android.widget.ShareActionProvider" />
</menu>
的活动: -
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.action_bar_share_menu, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
ShareActionProvider myShareActionProvider = (ShareActionProvider) item.getActionProvider();
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_SEND);
myIntent.putExtra(Intent.EXTRA_TEXT, "Whatever message you want to share");
myIntent.setType("text/plain");
myShareActionProvider.setShareIntent(myIntent);
return true;
答案 0 :(得分:1)
试试这个: -
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_location_found"
android:clickable="true"
android:icon="@drawable/ic_launcher"
android:showAsAction="always"
android:title="Share">
<menu>
<item
android:id="@+id/facebook"
android:orderInCategory="1"
android:showAsAction="never"
android:title="Facebook">
</item>
<item
android:id="@+id/twitter"
android:orderInCategory="2"
android:showAsAction="never"
android:title="Twitter">
</item>
<item
android:id="@+id/gplus"
android:orderInCategory="3"
android:showAsAction="never"
android:title="Google Plus">
</item>
<item
android:id="@+id/seeall"
android:orderInCategory="4"
android:showAsAction="never"
android:title="See All">
</item>
</menu>
</item>
</menu>
活动(Java代码): -
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.facebook:
Toast.makeText(getApplicationContext(), "Testing", 1000).show();
// write the code here, what you want the action when user click
// facebook submenu....
break;
case R.id.twitter:
break;
case R.id.gplus:
break;
case R.id.seeall:
break;
default:
return true;
}
return super.onOptionsItemSelected(item);
}
}
Kotlin代码: -
class MainActivity:Activity() {
protected fun onCreate(savedInstanceState:Bundle) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun onCreateOptionsMenu(menu:Menu):Boolean {
val inflater = getMenuInflater()
inflater.inflate(R.menu.main, menu)
return super.onCreateOptionsMenu(menu)
}
fun onOptionsItemSelected(item:MenuItem):Boolean {
when (item.getItemId()) {
R.id.facebook ->
Toast.makeText(getApplicationContext(), "Testing", 1000).show()
R.id.twitter ->
{ //code
}
R.id.gplus ->
{ //code
}
R.id.seeall ->
{ //code
}
else -> return true
}// write the code here, what you want the action when user click
// facebook submenu....
return super.onOptionsItemSelected(item)
}
}