using Action Bar Android development

℡╲_俬逩灬. 提交于 2019-12-25 07:16:55

问题


I am new to android development. I am working on a small project, The title of the app is supposed to be in the center and a textview on the left side of the action bar.

I would like to know if the approach is correct. 1. I have hidden the actionbar using

 ActionBar actionBar = getSupportActionBar();
 actionBar.hide(); 

2.I Developed a layout to replace the actionbar like this

So is this the correct was of doing it ? If not, what's the correct way ?


回答1:


Your way is correct but please go through the Slidenerd Material design tutorial. You can create your own toolbar with your specification.Following site help you: Slidenerd toolbar tutorial




回答2:


Add Custom Toolbar to your Layout

<android.support.v7.widget.Toolbar
            android:id="@+id/custom_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            >

            <TextView
                android:id="@+id/tv_edit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:textColor="@color/white"
                android:text="Edit"
                />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:padding="5dp"
                android:orientation="horizontal"
                >

                <TextView
                  android:id="@+id/tv_title"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:textStyle="bold"
                  android:textColor="@color/white"
                  android:text="Movies"
                  />


            </LinearLayout>

            </android.support.v7.widget.Toolbar>

and Add this Code to your Java Class , inside your onCreate()

Toolbar custom_toolbar= (Toolbar) findViewById(R.id.custom_toolbar);
setSupportActionBar(custom_toolbar);

Hope this works for you, best of luck....




回答3:


Yes,perfect.

A small change can be instead of hiding the existing Action Bar you can replace it with your custom view( for now let us assume it is custom_actionbar.xml).your code goes like this :

        ActionBar mActionBar = getActionBar();
        mActionBar.setDisplayShowHomeEnabled(false);
        mActionBar.setDisplayShowTitleEnabled(false);
        LayoutInflater mInflater = LayoutInflater.from(this);
        View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
        mActionBar.setCustomView(mCustomView);
        mActionBar.setDisplayShowCustomEnabled(true);



回答4:


Add the below code . It will work.

ActionBar actionBar = getActionBar();   
     actionBar.setDisplayShowCustomEnabled(true);
                LayoutInflater inflater = LayoutInflater.from(this);
              View v = inflater.inflate(R.layout.action_bar_title, null);
                TextView titleTextView = (TextView) v.findViewById(R.id.custom_action_bar_title);
                titleTextView.setText("Title");
                actionBar.setDisplayShowHomeEnabled(true);
                actionBar.setIcon(icon);
                actionBar.setCustomView(v);



回答5:


I will recommend you to use toolbar api's provided in Lollipop version of android but with getSupportActionbar(), use this-

    actionBar.setCustomView(mCustomView);
    actionBar.setDisplayShowCustomEnabled(true);



回答6:


Follow this way

 ActionBar mActionBar = getActionBar();
            mActionBar.setDisplayShowHomeEnabled(false);
            mActionBar.setDisplayShowTitleEnabled(false);
            LayoutInflater mInflater = LayoutInflater.from(this);

            View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
            TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text);
            mTitleTextView.setText("My Own Title");

            ImageButton imageButton = (ImageButton) mCustomView
                    .findViewById(R.id.imageButton);
            imageButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View view) {
                    Toast.makeText(getApplicationContext(), "Refresh Clicked!",
                            Toast.LENGTH_LONG).show();
                }
            });

            mActionBar.setCustomView(mCustomView);
            mActionBar.setDisplayShowCustomEnabled(true);

And The custom_actionbar.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@drawable/black_pattern" >

    <TextView
        android:id="@+id/title_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textAllCaps="true"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#fff"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="8dp"
        android:src="@drawable/ic_launcher" />

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="8dp"
        android:background="@null"
        android:src="@android:drawable/ic_menu_rotate" />

</RelativeLayout>

Hope this will helps you



来源:https://stackoverflow.com/questions/35550050/using-action-bar-android-development

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!