13 January 2017

Android: Options Menu Bar with Submenu - Tạo menu và menu lớp con P1

activity_main.xml
Java Android 2017
<?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"
    tools:context="com.example.lonely.demostyle.MainActivity">

    <TextView
        style="@style/Mystyle"
        android:text="@string/hello_world" />
</RelativeLayout>
main_menu.xml
Java Android 2017
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/setting"
        android:title="Setting"
        app:showAsAction="always" />
    <item
        android:id="@+id/about"
        android:title="About"
        app:showAsAction="always" />
    <item
        android:id="@+id/share"
        android:icon="@drawable/ic_action_share"
        app:showAsAction="always">
        <menu>
            <item
                android:id="@+id/facebook"
                android:title="facebook"
                android:icon="@drawable/ic_facebook"
                app:showAsAction="never" />
            <item
                android:id="@+id/yahoo"
                android:icon="@drawable/ic_yahoo"
                android:title="Yahoo"
                app:showAsAction="never" />
        </menu>

    </item>
    <item
        android:id="@+id/contact"
        android:title="Contact"
        app:showAsAction="never" />
</menu>
MainActivity.java
Java Android 2017
package com.example.lonely.demostyle;

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

public class MainActivity extends AppCompatActivity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }
}
styles.xml
Java Android 2017
<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>


    <style name="Mystyle">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:textColor">#ff0000</item>
        <item name="android:layout_centerHorizontal">true</item>
        <item name="android:textSize">30sp</item>
    </style>

</resources>
strings.xml
Java Android 2017
<resources>
    <string name="app_name">DemoStyle</string>
    <string name="hello_world">Hello World</string>
</resources>

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang