我在Android中与ActionBar挣扎。
继承我的问题: 我的Action项目没有显示在ActionBar中,相反它们被堆叠在动作溢出中,无论我做什么..
我花了一天时间寻找解决方案,但我似乎无法找到丢失的东西..:-s
我尝试将android:showAsAction设置为“Always”:它没有用。 当我将ifRoom更改为Always时,logcat说: testmenu.xml:5:错误:错误:不允许使用字符串类型(在'showAsAction'中,值为'Always')。 +它根本不驯服。
我在这里尝试了很多不同的词汇和帖子,但不幸的是我找不到解决方案..
继承我的代码:
Mainactivity.java package com.locklock;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
//import android.support.v7.app.ActionBar.Tab;
//import android.support.v7.app.ActionBarActivity;
//import android.support.v7.app.ActionBar;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements TabListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
// Set up the action bar to show tabs.
final ActionBar actionBar= getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// actionBar.setDisplayShowTitleEnabled(false);
// adding tabs
ActionBar.Tab tab1=actionBar.newTab();
tab1.setText("TEST");
tab1.setTabListener(this);
ActionBar.Tab tab2=actionBar.newTab();
tab2.setText("EVENTS");
tab2.setTabListener(this);
ActionBar.Tab tab3=actionBar.newTab();
tab3.setText("USERS");
tab3.setTabListener(this);
ActionBar.Tab tab4=actionBar.newTab();
tab4.setText("SETTINGS");
tab4.setTabListener(this);
actionBar.addTab(tab1);
actionBar.addTab(tab2);
actionBar.addTab(tab3);
actionBar.addTab(tab4);
// tab = getActionBar.newTab();
// tab.setText(tabText);
// tab.setIcon(R.drawable.tab_icon);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.action_users, menu);
//return true;
//MenuInflater Mymenu = getMenuInflater();
//Mymenu.inflate(R.menu.action_users, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id) {
case R.id.action_settings:
// todo
return true;
case R.id.action_add_user:
Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
return true;
case R.id.action_set_permission:
// todo
return true;
case R.id.action_delete_user:
// todo
return true;
default:
return super.onOptionsItemSelected(item);
}
//if (id == R.id.action_settings) {
// return true;
// }
// return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
Log.e("mytag","test");
}
@Override
public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
Log.d("mytag","xxxx");
}
@Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
//Log.d("mytag","xxxx");
}
}
action_users.xml
<!-- Add User -->
<item android:id="@+id/action_add_user"
android:icon="@drawable/ic_action_remove"
android:title="@string/add_user"
android:showAsAction="ifRoom" />
<!-- Set permission for a User-->
<item android:id="@+id/action_set_permission"
android:icon="@drawable/ic_action_remove"
android:title="@string/set_permission"
android:showAsAction="ifRoom" />
<!-- delete User-->
<item android:id="@+id/action_delete_user"
android:icon="@drawable/ic_action_remove"
android:title="@string/delete_user"
android:showAsAction="ifRoom" />
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.locklock"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="18"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:uiOptions="splitActionBarWhenNarrow" >
<activity
android:name="com.locklock.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
任何人都可以快速看看这个吗?
提前,非常感谢。
答案 0 :(得分:0)
尝试在yourapp
action_users.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
yourapp:showAsAction="ifRoom" />
...
</menu>
答案 1 :(得分:0)
在xml菜单文件中,我不得不改变这个:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myappname="http://schemas.android.com/apk/res-auto" >
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
myappname:showAsAction="always" />
</menu>
到此:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.myappname.MainActivity" >
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
app:showAsAction="always" />
</menu>
它有效。