简体   繁体   English

Android工具栏

[英]Android Toolbar

I am very confused about how to change from ActionBar to toolbar in my application. 我对如何在应用程序中从ActionBar更改为toolbar感到非常困惑。 I see a lot of documentation, and I cannot find any solution for my problem. 我看到了很多文档,但找不到针对我的问题的任何解决方案。 Any help with my code is welcome. 欢迎提供有关我的代码的任何帮助。 I need to change all my actionbar (deprecated) for the new toolbar and I cannot set the toolbar in the class that I have. 我需要为新工具栏更改所有操作actionbar (不建议使用),并且无法在已有的类中设置工具栏。 A clue to the problem is in my extends or implements. 这个问题的线索在于我的扩展或实现。 i don't know, thanks for the advice! 我不知道,谢谢你的建议!

public class Quizz_View extends FragmentActivity implements ActionBar.TabListener {

    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;
    private DrawerLayout leftDrawer;
    private ListView drawerListView;
    private ActionBar ldActionBar;
    private Toolbar toolBar;
    private ActionBarDrawerToggle abDrawerToggle;
 // private ArrayList<Class_Questions> questionsList = new
 // ArrayList<Class_Questions>();

    GridView gridView;
    float xInicial;
    float yInicial;
    float xActual;
    float yActual;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_pager);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      //setSupportActionBar(toolbar);       

      //menu del toolbar
      //toolbar.inflateMenu(R.menu.MENU);

      //toolbar.setTitle("AD NORMA");
      //toolbar.setSubtitle("INGENIAT");

        Button reply = (Button) findViewById(R.id.replyBtn);
        reply.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

            }
        });

        final Button finish = (Button) findViewById(R.id.finishBtn);
        finish.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
            }
        });

        finish.setVisibility(View.INVISIBLE);

        /********************/
        actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowHomeEnabled(true);
        // get ListView defined in activity_main.xml
        drawerListView = (ListView) findViewById(R.id.left_drawer);
        // crea_menu_navegacion();

        // Set the adapter for the list view
        // drawerListView.setAdapter();
        leftDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        abDrawerToggle = new ActionBarDrawerToggle(this, leftDrawer, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close);
        leftDrawer.setDrawerListener(abDrawerToggle);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        leftDrawer.setDrawerShadow(R.drawable.drawer_shadow,    GravityCompat.START);
        // drawerListView.setOnItemClickListener(new DrawerItemClickListener());
        /***********************/

        // Inicialización
        ArrayList<Integer> inte = new ArrayList<Integer>();
        viewPager = (ViewPager) findViewById(R.id.pager);
        // ArrayList<Integer> pageNumber = new ArrayList<Integer>();
        actionBar = getActionBar();

        try {

            // ENTRA A PAGINAS
            JSONObject jsonQuiz = new JSONObject(stringQuiz);
            jsonPaginas = jsonQuiz.getString("Paginas");
            JSONArray jsn = new JSONArray(jsonPaginas);
            mAdapter = new TabsPagerAdapter(getSupportFragmentManager(),    jsonPaginas);

            for (Integer i = 0; i < jsn.length(); i++) {
                actionBar.addTab(actionBar.newTab().setText("Página " + (i + 1)).setTabListener(this));         
            }

        } catch (Exception e) {
        }
        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {

                if (position >= 5)
                    finish.setVisibility(View.VISIBLE);
                else
                    finish.setVisibility(View.INVISIBLE);

                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }

    private ActionBar getSupportActionBar() {
        return null;
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {

        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    }
}

AND THE XML 和XML

<android.support.v4.widget.DrawerLayout /android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</android.support.v4.view.ViewPager>

<include
    android:id="@+id/toolbarr"
    layout="@layout/toolbarex"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:textSize="28sp" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:gravity="bottom"
    android:textSize="28sp" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:gravity="center"
    android:textSize="28sp" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:gravity="center_horizontal"
    android:textSize="28sp" />

<Button
    android:id="@+id/replyBtn"
    style="@style/buttons"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="31dp"
    android:layout_marginLeft="20dp"
    android:text="Responder" />

<Button
    android:id="@+id/finishBtn"
    style="@style/buttons"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="18dp"
    android:layout_marginRight="26dp"
    android:text="Terminar" />

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@android:color/darker_gray"
    android:choiceMode="singleChoice"
    android:divider="@android:color/background_dark"
    android:dividerHeight="1dp" />

Add toolbar in layout: 在布局中添加工具栏:

 <android.support.v7.widget.Toolbar
        android:layout_height=”wrap_content”
        android:layout_width=”match_parent”
        android:minHeight=”@dimen/triple_height_toolbar”
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

Change the following in styles.xml to remove action bar 在styles.xml中更改以下内容以删除操作栏

<style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">

    <item name="windowActionBar">false</item>

</style>

in MainActivity.java 在MainActivity.java中

Toolbar toolbar = (Toolbar) findViewById(R.id.tool1);

setSupportActionBar(toolbar);
toolbar.setTitle("ToolBar Demo");
toolbar.setLogo(R.drawable.ic_launcher);

Put the Toolbar Xml code before the Viewpager. 将工具栏Xml代码放在Viewpager之前。 The ViewPager may be taking the whole screen height and toolbar is getting behind it. ViewPager可能占用了整个屏幕的高度,而工具栏却在后面。

Just place the below code before viewpager instead of afterwards. 只需将以下代码放在viewpager之前,而不是之后。

<include 
 android:id="@+id/toolbarr" layout="@layout/toolbarex"    
 android:layout_width="match_parent" 
 android:layout_height="wrap_content"/>

One clarification: With the new version of the support library, you should be using AppCompatActivity 一项澄清:在新版本的支持库中,您应该使用AppCompatActivity

Step 1: Use a NoActionBar parent theme. 步骤1:使用NoActionBar父主题。

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

Step 2: Derive your activity from AppCompatActivity. 步骤2:从AppCompatActivity派生您的活动。

public class MainActivity extends AppCompatActivity

Step 3: Add a Toolbar into your hierarchy. 步骤3:将工具栏添加到层次结构中。

<android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

Step 4: Attach the Toolbar to your AppCompatActivity. 步骤4:将工具栏附加到您的AppCompatActivity。

In MainActivity: Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); 在MainActivity中: Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar);

You can now use getSupportActionBar() to use your toolbar like an ActionBar if you wish. 现在,您可以根据需要使用getSupportActionBar()来像ActionBar一样使用工具栏。

在此处输入图片说明

Here is the code for it. 这是它的代码。

Build.Gradle file Build.Gradle文件

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

styles.xml styles.xml

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

Create a new layout file toolbar.xml 创建一个新的布局文件toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/primary"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

activity_main.xml activity_main.xml中

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context="com.ahotbrew.toolbar.MainActivity">

    <include layout="@layout/toolbar"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
         android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin" >
        <TextView
            android:text="@string/hello_brew"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </RelativeLayout>
</LinearLayout>

MainActivity.java MainActivity.java

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        setSupportActionBar(toolbar);
    }

This is how you create a toolbar. 这是创建工具栏的方式。 The example is from http://www.ahotbrew.com/android-toolbar-example/ 该示例来自http://www.ahotbrew.com/android-toolbar-example/

If you need to show the toolbar at the bottom then you'll find the code snippet in the link. 如果您需要在底部显示工具栏,则可以在链接中找到代码片段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM