gpt4 book ai didi

android - Action 项目系统地堆叠在 Action 溢出中,在 Action 栏中

转载 作者:行者123 更新时间:2023-11-29 01:41:01 27 4
gpt4 key购买 nike

我正在为 android 中的 ActionBar 而苦苦挣扎。

这是我的问题:我的操作项没有显示在操作栏中,而是堆叠在操作溢出中,无论我做什么..

我花了一天的时间寻找解决方案,但我似乎找不到缺少的东西..:-s

我尝试将 android:showAsAction 设置为“始终”:它没有用。当我将 ifRoom 更改为 Always 时,logcat 说:testmenu.xml:5: 错误:错误:不允许字符串类型(在“showAsAction”中值为“Always”)。 + 它根本不露营。

我在这里尝试了很多不同的教程和帖子,但不幸的是我找不到解决方案..

这是我的代码:

主 Activity .java 包 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>

有人可以快速查看一下吗?

提前,非常感谢。

最佳答案

经过几天的测试,我终于找到了答案。

在 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>

它奏效了。

关于android - Action 项目系统地堆叠在 Action 溢出中,在 Action 栏中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24678093/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com