私は、さまざまな画像ボタンとメインタイトルを持つ多くの画面(アクティビティ)でツールバーを使用しているアプリで作業しています。このためには、ツールバーを作成し、それを対応する画面の.xmlファイルに含めるために、すべての画面に.xmlを作成する必要があります。私は、各スクリーンの各ツールバーを作成せずに、ツールバーに1つの.xmlファイルのみを使用して、関連する画面用にプログラムでも1つの.xmlでも編集(タイトルの変更またはボタンの追加を意味します)します。何か方法はありますか?以下は私のコードです。Androidアプリの異なる画面用に1つのツールバーを作成する
<?xml version="1.0" encoding="utf-8"?>
<!--Main Activity toolbar-->
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@color/barColor"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="MAIN ACTIVITY"
android:textColor="#ffffff"
android:id="@+id/mytext"
android:textSize="18sp" />
<ImageButton
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:src="@drawable/back"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scaleType="centerInside"
android:onClick="GoBack"
android:background="@null"
/>
</android.support.v7.widget.Toolbar>
メインスクリーンデザインの.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="match_parent"
android:background="#3A3A3B"
android:orientation="vertical">
<!--Including the toolbar in main screen here-->
<include
android:id="@+id/mainbar"
layout="@layout/mainactivitytoolbar"
/>
<TextureView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scanView"
android:layout_centerHorizontal="true"
android:layout_below="@+id/mainbar" />
</RelativeLayout>
私は1つ以上のImageButtonで別の画面にこのツールバーを追加する必要がある場合、私はこのような別のツールバー.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="40dp"
android:background="@color/barColor"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="SECOND ACTIVITY"
android:textColor="#ffffff"
android:id="@+id/mytext"
android:textSize="18sp" />
<ImageButton
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:src="@drawable/back"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scaleType="centerInside"
android:onClick="Back"
android:background="@null"
/>
<!--New Image button added-->
<ImageButton
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@drawable/serial"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_gravity="right"
android:scaleType="centerInside"
android:background="@null"
android:onClick="Enter"
/>
</android.support.v7.widget.Toolbar>
ツールバーのタイトルのためにあなたがプログラムでそれを変更し、アイコンの1ツールバーのすべてのアイコンを作成し、あなたの要求に応じてオン/オフ彼らの表示を切り替えることができます –
しかし、私はツールバーの同じ場所(右コーナー)でアイコンが必要です、それはまだ可能ですか? – WasimSafdar
はい、その場合は 'Toolbar'ではなく' Relative layout'を使用する必要があります –