应用程序

时间:2016-12-19 23:29:20

标签: android xml

大家好,所以我的问题是菜单甚至没有出现,我尝试过这样的事情:app:showAsAction="always"但它没有帮助,然后我尝试更改tools:context=".Main" 这是我的完整代码:

main_menu.xml

<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=".Main">



<item
    android:id="@+id/changeCityID"
    android:title="@string/changeCity"
    android:icon="@drawable/search"
    android:orderInCategory="1"
    app:showAsAction="always"/>

</menu>

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background= "#ff1ba1ee"
tools:context="weather.hyper.com.weather.Main">

<TextView
    android:id="@+id/cityText"
    android:textColor="#fff"
    android:text="Zagreb, HRV"
    android:layout_centerHorizontal="true"
    android:textSize="18sp"
    android:textStyle="bold"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />


<ImageView
    android:id="@+id/vrijemeIcon"
    android:layout_below="@+id/cityText"
    android:layout_marginTop="10dp"
    android:background="#fff"
    android:layout_width="100dp"
    android:layout_height="100dp" />

<TextView
    android:id="@+id/temparaturaText"
    android:text="12°C"
    android:textStyle="normal"
    android:textColor="#fff"
    android:layout_below="@+id/cityText"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="25dp"
    android:textSize="42sp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/windText"
    android:textColor="#fff"
    android:textSize="16sp"
    android:text="Vjetar"
    android:layout_below="@id/vrijemeIcon"
    android:layout_marginTop="35dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/cloudText"
    android:text="Oblaci"
    android:layout_below="@+id/windText"
    android:layout_marginTop="5dp"
    android:textColor="#fff"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/tlakText"
    android:textSize="16sp"
    android:layout_marginTop="5dp"
    android:textColor="#fff"
    android:text="Tlak"
    android:layout_below="@+id/cloudText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/humidText"
    android:textSize="16sp"
    android:layout_marginTop="5dp"
    android:textColor="#fff"
    android:text="Humidity"
    android:layout_below="@+id/tlakText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/riseText"
    android:textSize="16sp"
    android:layout_marginTop="5dp"
    android:textColor="#fff"
    android:text="Sunrise"
    android:layout_below="@+id/humidText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/setText"
    android:textSize="16sp"
    android:layout_marginTop="5dp"
    android:textColor="#fff"
    android:text="Sunset"
    android:layout_below="@+id/riseText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/updateText"
    android:textSize="16sp"
    android:layout_marginTop="5dp"
    android:textColor="#fff"
    android:text="Zadnji update"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/setText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</RelativeLayout>

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="weather.hyper.com.weather">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".Main">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

代码看起来像这样

`package weather.hyper.com.weather;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class Main extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    invalidateOptionsMenu();
    setContentView(R.layout.activity_main);
}

public boolean onCreateOptionsMenu(Menu menu){
    getMenuInflater().inflate(R.menu.main_menu, menu);
    return super.onCreateOptionsMenu(menu);
}

public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()){
        case R.id.changeCityID:
            //
            break;
    }
    return true;
}



}`

3 个答案:

答案 0 :(得分:0)

验证您是否在主要活动中实施onCreateOptionsMenu和onOptionsItemSelected方法

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater inflater = getMenuInflater();

    inflater.inflate(R.menu.main_menu, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    switch (item.getItemId())
    {
      case R.id.changeCityID:
        //doSomething
        break;
    }

    return true;
}

答案 1 :(得分:0)

您没有显示您的MainActivity代码。您的活动应该调用main_menu.xml文件。确保main_menu.xml位于菜单文件夹下。然后在您想要此菜单的活动(或您的应用程序屏幕)中,确保您有类似于此实现的内容;

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
  version="1.0"
  xmlns="http://schemas.microsoft.com/wix/2006/wi"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:wix="http://schemas.microsoft.com/wix/2006/wi"
  xmlns:str="http://xsltsl.org/string"
  exclude-result-prefixes="wix str"  
  >

  <xsl:output
    encoding="utf-8"
    method="xml"
    version="1.0"
    indent="yes"    
    />

  <xsl:template match='wix:Component[contains(wix:File/@Source, "SourceDir\Prog.exe")]'> 
    <!-- assumes there is only one Prog.exe -->
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
      <xsl:comment> added shortcut under Component with File that has Source with Prog.exe </xsl:comment>
      <!-- Elsewhere, have an Icon element like: <Icon Id="Prog.exe" SourceFile="$(var.BUILDCACHE)Bin/Prog.exe" />  -->
      <Shortcut 
        Id="ProgExeShortcut" 
        Name="Prog Application" 
        Icon="Prog.exe" 
        Directory="ProgramMenuFolder_ProgVendor" 
        Advertise="yes">
        <xsl:attribute name="WorkingDirectory"><xsl:value-of select="@Directory"/></xsl:attribute>
      </Shortcut>
      <RemoveFolder 
        Id="ProgExeShortcut_ProgramMenuFolder_ProgVendor"  
        Directory="ProgramMenuFolder_ProgVendor" 
        On="uninstall" />
    </xsl:copy>
  </xsl:template>

    <!-- identity template -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match='/'>
    <xsl:comment>*** DO NOT EDIT: Generated by heat.exe; transformed by ProgComponentGroup.xsl</xsl:comment>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:template>

</xsl:stylesheet>

答案 2 :(得分:0)

将此行放在onCreate(....)

invalidateOptionsMenu();

调用Android菜单,并实现菜单方法

public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);
    return super.onCreateOptionsMenu(menu);
}