onzulin

Android actionbar

Jun 19th, 2017
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.39 KB | None | 0 0
  1. Este codigo es de la actividad principal donde pongo la Toolbar
  2. <!-- /res/layout/activity_main.xml -->
  3. <?xml version="1.0" encoding="utf-8"?>
  4. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5.     xmlns:app="http://schemas.android.com/apk/res-auto"
  6.     xmlns:tools="http://schemas.android.com/tools"
  7.     android:layout_width="match_parent"
  8.     android:layout_height="match_parent"
  9.     tools:context="com.techcomputerworld.tcw.test_menu.MainActivity">
  10.  
  11.  
  12.     <android.support.v7.widget.Toolbar
  13.         android:id="@+id/toolbar"
  14.         android:layout_width="match_parent"
  15.         android:layout_height="wrap_content"
  16.         android:background="?attr/colorPrimary"
  17.         android:minHeight="?attr/actionBarSize"
  18.         android:elevation="4dp"
  19.         android:theme="?attr/actionBarTheme"
  20.         android:layout_alignParentTop="true"
  21.         android:layout_alignParentStart="true"
  22.         app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
  23.         />
  24. </RelativeLayout>
  25. /*
  26.  * otra layout que define el menu que solo he puesto uno
  27.  */
  28. <!-- /res/menu/actionmenu.xml -->
  29. <?xml version="1.0" encoding="utf-8"?>
  30. <menu xmlns:android="http://schemas.android.com/apk/res/android"
  31.     xmlns:app="http://schemas.android.com/apk/res-auto">
  32.     <!-- "Mark Favorite", should appear as action button if possible -->
  33.     <item
  34.         android:id="@+id/action_favorite"
  35.         android:icon="@drawable/ic_favorite_black"
  36.         android:title="@string/action_favorite"
  37.         app:showAsAction="ifRoom"/>
  38.  
  39.     <!-- Settings, should always be in the overflow -->
  40.     <item android:id="@+id/action_settings"
  41.         android:title="@string/action_settings"
  42.         app:showAsAction="never"/>
  43.  
  44. </menu>
  45. /*
  46.  * Aqui ya pongo codigo en Java para que lo veais y tambien pondre el manifiesto de la aplicacion que es importante añadir la linea que           hay que añadir  
  47. mainactivity.java
  48.  */
  49. package com.techcomputerworld.tcw.test_menu;
  50.  
  51. import android.support.v7.app.ActionBar;
  52. import android.support.v7.app.AppCompatActivity;
  53.  
  54.  
  55. import android.os.Bundle;
  56. import android.support.v7.widget.Toolbar;
  57. import android.view.MenuItem;
  58.  
  59. public class MainActivity extends AppCompatActivity {
  60.  
  61.     @Override
  62.     protected void onCreate(Bundle savedInstanceState) {
  63.         super.onCreate(savedInstanceState);
  64.         setContentView(R.layout.activity_main);
  65.  
  66.         Toolbar maintoolbar = (Toolbar)findViewById(R.id.toolbar);
  67.         setSupportActionBar(maintoolbar);
  68.  
  69.         //obtener el soporte correspondiente a la toolbar
  70.         ActionBar ab = getSupportActionBar();
  71.  
  72.         //Enable the Up Button
  73.         ab.setDisplayHomeAsUpEnabled(true);
  74.  
  75.     }
  76.     @Override
  77.     public boolean onOptionsItemSelected(MenuItem item) {
  78.         switch (item.getItemId()) {
  79.             case R.id.action_settings:
  80.                 // User chose the "Settings" item, show the app settings UI...
  81.                 return true;
  82.  
  83.             case R.id.action_favorite:
  84.                 // User chose the "Favorite" action, mark the current item
  85.                 // as a favorite...
  86.                 return true;
  87.  
  88.             default:
  89.                 // If we got here, the user's action was not recognized.
  90.                 // Invoke the superclass to handle it.
  91.                 return super.onOptionsItemSelected(item);
  92.  
  93.         }
  94.     }
  95. }
  96. /*
  97. aqui pongo otro fichero xml
  98. androidmanifest.xml
  99. */
  100. <?xml version="1.0" encoding="utf-8"?>
  101. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  102.     package="com.techcomputerworld.tcw.test_menu">
  103.  
  104.     <application
  105.         android:allowBackup="true"
  106.         android:icon="@mipmap/ic_launcher"
  107.         android:label="@string/app_name"
  108.         android:roundIcon="@mipmap/ic_launcher_round"
  109.         android:supportsRtl="true"
  110.         android:theme="@style/Theme.AppCompat.Light.NoActionBar">
  111.         <!--android:theme="@style/Theme.AppCompat.DayNight.NoActionBar"-->
  112.  
  113.         <activity android:name=".MainActivity"
  114.             android:parentActivityName=".MainActivity">
  115.  
  116.             <intent-filter>
  117.                 <action android:name="android.intent.action.MAIN" />
  118.  
  119.                 <category android:name="android.intent.category.LAUNCHER" />
  120.             </intent-filter>
  121.         </activity>
  122.     </application>
  123.  
  124. </manifest>
  125. Esta claro que en algun sitio esta el error, si pudierais revisarlo porque fijo que es un error tonto de novato.
Advertisement
Add Comment
Please, Sign In to add comment