Android
 Computer >> コンピューター >  >> プログラミング >> Android

Androidでカスタムアクションバーを作成するにはどうすればよいですか?


例に入る前に、Androidのアクションバーとは何かを知っておく必要があります。 Androidのヘッダーと同じようなアクションバー。すべての画面に同じアクションバーを使用することも、特定のアクティビティに合わせてアクションバーを変更することもできます。

この例は、Androidでカスタムアクションバーを作成する方法を示しています。

ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。

ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "https://schemas.android.com/apk/res/android"
   xmlns:tools = "https://schemas.android.com/tools"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   tools:context = ".MainActivity">
   <TextView
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content"
      android:text = "Custom Action Bar"
      android:textSize = "20sp"/>
</LinearLayout>

ステップ2 −次のコードをsrc / MainActivity.java

に追加します
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      this.getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
      getSupportActionBar().setDisplayShowCustomEnabled(true);
      getSupportActionBar().setCustomView(R.layout.custom_action_bar);
      //getSupportActionBar().setElevation(0);
      View view = getSupportActionBar().getCustomView();
      TextView name = view.findViewById(R.id.name);
      name.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Toast.makeText(MainActivity.this, "You have clicked tittle", Toast.LENGTH_LONG).show();
         }
      });
   }
}

ステップ3 −以下に示すcustom_action_bar.xmlとして、resフォルダーにアクションバーのレイアウトを作成します

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "https://schemas.android.com/apk/res/android"
   xmlns:app = "https://schemas.android.com/apk/res-auto"
   android:layout_width = "match_parent"
   android:layout_height = "wrap_content"
   android:gravity = "center_vertical"
   android:padding = "10dp"
   android:weightSum = "1">
   <LinearLayout
      android:layout_width = "0dp"
      android:layout_height = "match_parent"
      android:layout_weight = "0.6">
      <ImageView
         android:layout_width = "wrap_content"
         android:layout_height = "match_parent"
         android:src = "@drawable/ic_face_red_400_24dp" />
      <TextView
         android:id = "@+id/name"
         android:layout_width = "match_parent"
         android:layout_height = "wrap_content"
         android:layout_marginLeft = "10dp"
         android:text = "Instagram"
         android:textSize = "20sp"
         android:textColor = "#000"
         android:textStyle = "bold"
         app:fontFamily = "@font/allan_bold" />
   </LinearLayout>
   <LinearLayout
      android:layout_width = "0dp"
      android:layout_height = "match_parent"
      android:layout_marginRight = "10dp"
      android:layout_weight = "0.4"
      android:gravity = "end">
   <ImageView
      android:layout_width = "wrap_content"
      android:layout_height = "match_parent"
      android:src = "@drawable/ic_local_post_office_red_400_24dp" />
   <ImageView
      android:layout_width = "wrap_content"
      android:layout_height = "match_parent"
      android:layout_marginLeft = "20dp"
      android:src = "@drawable/ic_send_red_400_24dp" />
   </LinearLayout>
</LinearLayout>

−プロジェクト/アプリケーションの仕様に従って、カスタムレイアウトを変更する必要があります。

Androidでカスタムアクションバーを作成するにはどうすればよいですか?

アプリケーションを実行してみましょう。実際のAndroidモバイルデバイスをコンピューターに接続していると思います。 android studioからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、[実行]をクリックします ツールバーのAndroidでカスタムアクションバーを作成するにはどうすればよいですか? アイコン。オプションとしてモバイルデバイスを選択し、デフォルトの画面を表示するモバイルデバイスを確認します。

アクションバーのボタンの影を削除するには、以下に示すように、MainActivityのonCreate()で次のコードを使用します

this.getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setCustomView(R.layout.custom_action_bar);
getSupportActionBar().setElevation(0);
View view = getSupportActionBar().getCustomView();

アプリケーションを実行すると、次のような出力が得られます-

Androidでカスタムアクションバーを作成するにはどうすればよいですか?


  1. FacebookでAndroidアプリを作成する方法は?

    この例は、FacebookでAndroidアプリを作成する方法を示しています。FacebookアプリIDを取得するには、Facebook開発者サイトでFacebookアプリを作成する必要があります。次の手順を1つずつ実行してください。 https://developers.facebook.com/にアクセスして、新しいアプリを追加します。 ステップ1 -指定されたフィールドにアプリ名とメールアドレスを入力します ステップ2 –以下の行build.gradle/mavenを追加します Maven中央リポジトリからSDKをダウンロードします: buildscript { reposi

  2. Androidで透過的なステータスバーとアクションバーを作成するにはどうすればよいですか?

    この例は、Androidで透過的なステータスバーとアクションバーを作成する方法を示しています。 ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。 ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。 <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.Co