2015-10-20 10 views
10

私はAndroid開発が初めてで、カスタムツールバーの作成に大きな問題があります。私の要件:カスタムボタン 左側のカスタムボタンを使用してツールバーを作成するにはどうすればよいですか?

  • ボタンの高さの後に左の

    1. カスタムボタン(アイコン+テキスト)
    2. 分周器は、ここで(マージンなし)

    がサンプルされていないツールバーと同じにする必要があります enter image description here

    私はactionBar.setCustomView(v);を使用しようとしていましたが、私の問題は解決しませんでした:

    1. 右のボタンは、トップ/ボトムマージンを持っている - 彼らは私が仕切りを追加することができませんでしたツールバー
    2. よりも小さくなっています。
    3. 左ボタン(カスタムビューから)がツールバーの高さより小さかった。

    私の質問:

    1. 私は本当に左側にカスタムボタンを追加するには、カスタムビューが必要ですか?
    2. 左側に仕切りを追加するにはどうすればよいですか?
    3. ボタンの高さをツールバーの高さと同じにするにはどうすればいいですか?
  • +0

    あなたが提供するソリューションをテストするためにあらゆる機会を持っていましたか? – reVerse

    答えて

    23

    Toolbarは基本的にFrameLayoutあるので、あなたが好きなレイアウトタグの内側に追加することができます。あなたのケースでは、以下のようなものは十分なようだ:

    layout.xml

    があなたの/res/drawableフォルダにこれを追加

    <android.support.v7.widget.Toolbar 
        android:id="@+id/toolbar" 
        android:layout_width="match_parent" 
        android:layout_height="?actionBarSize" 
        android:background="?colorPrimary" 
        app:contentInsetLeft="0dp" 
        app:contentInsetStart="0dp" 
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"> 
    
        <LinearLayout 
         android:layout_width="wrap_content" 
         android:layout_height="?attr/actionBarSize" 
         android:divider="@drawable/divider" 
         android:dividerPadding="8dp" 
         android:orientation="horizontal" 
         android:showDividers="end"> 
    
         <TextView 
          android:id="@+id/toolbar_save" 
          style="@style/TextAppearance.Widget.AppCompat.Toolbar.Subtitle" 
          android:layout_width="match_parent" 
          android:layout_height="match_parent" 
          android:background="?attr/selectableItemBackground" 
          android:drawableLeft="@drawable/ic_action_check" 
          android:drawablePadding="8dp" 
          android:gravity="center_vertical" 
          android:paddingLeft="16dp" 
          android:paddingRight="16dp" 
          android:text="Save" 
          android:textAllCaps="true" /> 
    
        </LinearLayout> 
    </android.support.v7.widget.Toolbar> 
    

    divider.xml。これは上のコードのLinearLayoutディバイダとして使用されます。

    <?xml version="1.0" encoding="utf-8"?> 
    <shape xmlns:android="http://schemas.android.com/apk/res/android" 
        android:shape="rectangle"> 
    
        <size android:width="1dp" /> 
    
        <solid android:color="@android:color/white" /> 
    
    </shape> 
    

    コード右側の項目の観点から

    private void setupToolbar() { 
        Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar); 
        setSupportActionBar(mToolbar); 
        // Hide the title 
        getSupportActionBar().setTitle(null); 
        // Set onClickListener to customView 
        TextView tvSave = (TextView) findViewById(R.id.toolbar_save); 
        tvSave.setOnClickListener(new View.OnClickListener() { 
         @Override 
         public void onClick(View v) { 
          // TODO 
         } 
        }); 
    } 
    

    :単にデフォルトonCreateOptionsMenuメソッドを使用して、対応するR.menu.*リソースを膨らませます。

    結果は

    result image

    +1

    app:contentInsetStart = "0dp"これが私の問題を解決しました – umesh

    0
    <android.support.v7.widget.Toolbar 
         android:layout_width="match_parent" 
         android:layout_height="?actionBarSize" 
         app:contentInsetLeft="0dp" 
         app:contentInsetStart="0dp" 
         app:contentInsetStartWithNavigation="0dp" 
         /> 
    

    はまた、アプリが必要:contentInsetStartWithNavigationは= "0dp" ツールバーする

    関連する問題