2016-04-15 14 views
0

私はXMLのアプリのためのツールバーを設計しようとしているが、困難に直面している。だから私は持っているコードは、これまでのところ、私はそれに機能を追加するために使用することができますので、私はツールバーを挿入しない方法アンドロイドのレイアウトを改善するには?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#cacaca" 

    /> 

のですか?それはXMLを使用して行う必要がありますか?またはこれを行う別の方法がありますか?

+1

[RTFM](http://developer.android .com/intl/ru/training/appbar/setting-up.html)。 – Marko

答えて

1

は、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:local="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="60dp" 
    android:background="@color/primary" 
    /> 

のような別のXMLファイルで作成する必要があり、すべての まず、あなたが使用する必要がありますあなたのレイアウトでは、このあなたがあなたの活動でそれを定義する必要がその後、この

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

よう

import android.support.v7.widget.Toolbar; 

private Toolbar nToolbar; 

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_create_add_subject); 
     this.setToolBar(); 
} 
private void setToolBar() { 
     nToolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(nToolbar); 
     getSupportActionBar().setDisplayShowHomeEnabled(true); 
     getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     setTitle("Title"); 
    } 
+0

@asdweここでは、ツールバーのコードの特定の行だけを書きます。残りのコードは、書く必要があります。 –

0

あなたがこの方法を実装して、ツールバーの助けを借りて、カスタムレイアウトを定義したい場合は、「saurabhグプタ」 への答えに追加:

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:local="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="60dp" 
     android:background="@color/primary"> 
     <!-- Your own implementation of layout --> 
     // .. 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="#cacaca" 
     /> 
     .. // 
</android.support.v7.widget.Toolbar>