如何在Android中建立自定義操作欄?
在進入示例之前,我們應該瞭解Android中的操作欄是什麼。操作欄就像Android中的頁首。我們可以對所有螢幕使用相同的操作欄,也可以為特定活動更改操作欄。
此示例演示瞭如何在Android中建立自定義操作欄。
步驟1 - 在Android Studio中建立一個新專案,轉到檔案⇒新建專案,並填寫建立新專案所需的所有詳細資訊。
步驟2 - 將以下程式碼新增到res/layout/activity_main.xml中。
<?xml version = "1.0" encoding = "utf-8"?> <LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://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 - 在res資料夾中為操作欄建立一個佈局,作為custom_action_bar.xml,如下所示
<?xml version = "1.0" encoding = "utf-8"?> <LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:app = "http://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 Studio執行應用程式,請開啟專案的活動檔案之一,然後單擊執行 工具欄中的圖示。選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示您的預設螢幕。
要刪除操作欄按鈕陰影,請在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();
現在執行您的應用程式,它將給出如下所示的輸出 -
點選此處下載專案程式碼
廣告