android活动不仅仅显示在主要活动中而且没有正确显示

时间:2014-09-11 07:40:25

标签: java android mobile

操作栏未显示在主要活动中,而相同的代码显示为另一个活动而另一个操作栏溢出未显示在右下角的列表中

  1. 图片1:按下菜单键的主要活动
  2. enter image description here

    1. 图片2:显示消息活动
    2. enter image description here

      主要活动代码:

      public class MainActivity extends ActionBarActivity {
      EditText t;
      public final static String sendMessageextra="com.example.example2.textmessage";
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          t=(EditText) findViewById(R.id.textmessage);
      }
      
      
      
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
      
          getMenuInflater().inflate(R.menu.activity_main_actions, menu);
          return super.onCreateOptionsMenu(menu);
      }
      
      public void sendMessage(View v){
          Intent sendmessageintent=new Intent(this,showMessage.class);
          String messagestring=t.getText().toString();
          sendmessageintent.putExtra(sendMessageextra, messagestring);
          startActivity(sendmessageintent);
      }
      

      }

      显示消息活动:

      public class showMessage extends Activity{
      TextView t;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.sendmessage);
          t=(TextView) findViewById(R.id.showmessage);
          showmessage();
      }
      
      
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          super.onCreateOptionsMenu(menu);
          MenuInflater inflater=getMenuInflater();
          inflater.inflate(R.menu.activity_main_actions, menu);
          return true;
      }
      
      
      public void showmessage(){
          Intent intent=getIntent();
          String message=intent.getStringExtra(MainActivity.sendMessageextra);
          t.setText(message);
          Log.i("Set New Text Box", "Text BOx");
      }
      

      }

      activity main actions.xml:

      <?xml version="1.0" encoding="utf-8"?>
      <menu xmlns:android="http://schemas.android.com/apk/res/android" >
      <!-- Search Widget -->
      <item android:id="@+id/action_search"
            android:icon="@drawable/ic_action_search"
            android:title="@string/action_search"
            android:showAsAction="ifRoom"
            android:actionViewClass="android.widget.SearchView"/>
      
      <!-- Location Found -->
      <item android:id="@+id/action_location_found"
            android:icon="@drawable/ic_action_location_found"
            android:title="@string/action_location_found"
            android:showAsAction="never" />
      <!-- Help -->
      <item android:id="@+id/action_help"
            android:icon="@drawable/ic_action_help"
            android:title="@string/action_help" 
            android:showAsAction="never"/>
      </menu>
      

      android menifest文件: 活动清单文件:

      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.example2"
          android:versionCode="1"
          android:versionName="1.0" >
          <uses-sdk
              android:minSdkVersion="8"
              android:targetSdkVersion="19" />
          <application
              android:allowBackup="true"
              android:icon="@drawable/ic_launcher"
              android:label="@string/app_name"
              android:theme="@style/AppTheme">
              <activity
                  android:name=".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>
              <activity
                  android:name="com.example.example2.showMessage"
                  android:label="Show Message" android:parentActivityName="com.example.example2.MainActivity"/>
          </application>
      </manifest>
      

2 个答案:

答案 0 :(得分:1)

如果你想在每个活动的动作栏右侧显示搜索图标,那么只需在actions.xml中进行更改而不是android:showAsAction =&#34; ifRoom&#34;将其更改为android:showAsAction =&#34;始终&#34;

答案 1 :(得分:0)

  1. ActionBar未显示
  2. 在你的oncreate()

    中这样做
    ActionBar actionBar = getActionBar();
    actionBar.show();
    
    1. ActionBar overflow 在那里你可以看到你的菜单
    2. 要显示菜单,请在活动主动作文件中

      <menu xmlns:android="http://schemas.android.com/apk/res/android"
                 xmlns:app="http://schemas.android.com/apk/res-auto" >
          <item
              android:id="@+id/action_search"
              android:showAsAction="always"
              android:title="@string/action_search"
              android:icon="@android:drawable/ic_action_search"
              android:actionViewClass="android.widget.SearchView"/>
      
      </menu>
      

      像这样你需要在主动作文件中进行更改