如何在 Android 中以编程方式创建菜单?
现在我想在屏幕底部添加一个菜单。 我写了很多,但仍然不知道如何做到这一点。 我的主要问题是我的主页上没有 xml 文件。 它看起来像这样:
public class start extends ListActivity {
static final String[] COUNTRIES = new String[] {
"NEWS1", "NEWS2","RADIO"};
Intent intent;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, COUNTRIES));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (((TextView) view).getText().equals("NEWS1")){
intent = new Intent(start.this, NewsActivity.class);
我如何添加带有操作的菜单。 请举个例子。 谢谢
Now I want to add it a menu on the bottom of the screen.
I wrote a lot of about but still didnt get how to do that.
My main problem is that I dont have an xml file on my main page.
its look liks that:
public class start extends ListActivity {
static final String[] COUNTRIES = new String[] {
"NEWS1", "NEWS2","RADIO"};
Intent intent;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, COUNTRIES));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (((TextView) view).getText().equals("NEWS1")){
intent = new Intent(start.this, NewsActivity.class);
how can I add a menu with an action.
please give me a example.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用此代码动态添加菜单
use this code to add menu dynamically
如果您的主要活动中没有 XML 文件,那也没关系。您仍然可以按照以下步骤添加菜单 ->
现在在您的主要活动代码中添加这两个方法 ->
Ya its fine if you dont have an XML file in your main activity. You can still add menu to it by following this steps ->
Now in your main activity code add this two methods ->
对于上下文菜单,您只需添加
上下文菜单仅在长按列表项时出现
For a context menu you just add
The context menu just appears on long click to the list item
对于选项菜单,
创建一个在选项菜单中膨胀的 android xml 文件
For options menu
create an android xml file that you inflate in options menu