在Android工具栏中创建按钮

alan_derua

我如何在Android的内部创建Toolbar类似于此iOS示例的按钮

iOS示例

马查多

带有按钮教程的工具栏

1-在内部添加库兼容性 build.gradle

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
}

2-创建文件名color.xml以定义Toolbar颜色

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="ColorPrimary">#FF5722</color>
    <color name="ColorPrimaryDark">#E64A19</color>
</resources>

3-修改style.xml文件

<resources>     
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

        <item name="colorPrimary">@color/ColorPrimary</item>
        <item name="colorPrimaryDark">@color/ColorPrimaryDark</item>
        <!-- Customize your theme here. -->
    </style>     
</resources>

4-创建一个类似的xml文件 tool_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
 xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:elevation="4dp" />

5-将Toolbar纳入您的main_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <include
        android:id="@+id/tool_bar"
        layout="@layout/tool_bar" />

    <TextView
        android:layout_below="@+id/tool_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/TextDimTop"
        android:text="@string/hello_world" />

</RelativeLayout>

6-然后,将其放在MainActivity班级中

package com.example.hp1.materialtoolbar;

import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

/* When using AppCompat support library                                                             
 * (you need to extend Main Activity to                                                            
 * ActionBarActivity)
 * ActionBarActivity has deprecated, use AppCompatActivity
 */
public class MainActivity extends ActionBarActivity { 
    // Declaring the Toolbar Object
    private Toolbar toolbar;

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

        // Attaching the layout to the toolbar object
        toolbar = (Toolbar) findViewById(R.id.tool_bar);
        // Setting toolbar as the ActionBar with setSupportActionBar() call
        setSupportActionBar(toolbar);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

7-最后,将“按钮项”添加到目录menu_main.xml内部/res/menu/

<?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"
        xmlns:tools="http://schemas.android.com/tools"
        tools:context=".MainActivity">
        <item
            android:id="@+id/action_settings"
            android:orderInCategory="100"
            android:title="@string/action_settings"
            app:showAsAction="never" />
        <item
            android:id="@+id/action_search"
            android:orderInCategory="200"
            android:title="Search"
            android:icon="@drawable/ic_search"
            app:showAsAction="ifRoom"/>                
        <item
            android:id="@+id/action_user"
            android:orderInCategory="300"
            android:title="User"
            android:icon="@drawable/ic_user"
            app:showAsAction="ifRoom" />        
    </menu>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

ubuntu touch工具栏中的按钮

来自分类Dev

ubuntu touch工具栏中的按钮

来自分类Dev

如何访问工具栏中的按钮?

来自分类Dev

在片段中创建工具栏

来自分类Dev

在片段中创建工具栏

来自分类Dev

在Android工具栏中显示图标

来自分类Dev

在Android工具栏中显示图标

来自分类Dev

在Android工具栏中查看

来自分类Dev

在android中更改工具栏标题

来自分类Dev

Firefox工具栏上的“创建”按钮以执行JS

来自分类Dev

Firefox工具栏上的“创建”按钮以执行JS

来自分类Dev

如何创建工具栏按钮以启动运行配置?

来自分类Dev

快速将工具栏按钮移到工具栏中的左侧

来自分类Dev

Excel工具栏按钮

来自分类Dev

android:如何在折叠的工具栏中添加带有文本的按钮

来自分类常见问题

如何在Android中更改工具栏后退按钮的颜色?

来自分类Dev

如何在Android的工具栏中添加刷新和搜索之类的按钮?

来自分类Dev

如何在Android的v7工具栏中删除后退按钮?

来自分类Dev

如何在Android材质组件中更改工具栏的后退按钮图标

来自分类Dev

在linearLayout(android kotlin)中的工具栏顶部放置一个按钮

来自分类Dev

如何实现android工具栏后退按钮

来自分类Dev

Android Material Design工具栏和FAB按钮交叉

来自分类Dev

Android工具栏:启用默认的向上按钮

来自分类Dev

Android工具栏主页按钮未显示

来自分类Dev

Android自定义工具栏文本按钮

来自分类Dev

Android工具栏:启用默认的向上按钮

来自分类Dev

Android:工具栏上的向上按钮处的右边距

来自分类Dev

工具栏按钮重力以扩展工具栏

来自分类Dev

如何配置MatPlotLib工具栏,使工具栏中的按钮为ttk按钮,而不是旧的Tk按钮

Related 相关文章

热门标签

归档