कस्टम लेआउट के साथ एक्शन बार पर्सनलाइज़ करें
यह उदाहरण दर्शाता है कि कस्टम लेआउट फाइल का उपयोग करके एक्शन बार में कस्टम व्यू (लोगो, शीर्षक, आइकन) कैसे सेट करें।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनें।
चरण 2: मेन लेआउट
res/layout/activity_main.xml - साधारण लेआउट।
चरण 3: कस्टम एक्शन बार लेआउट बनाएं
res/layout/custom_action_bar.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
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" />
<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" />
</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" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="20dp" />
</LinearLayout>
</LinearLayout>
चरण 4: MainActivity.java में कस्टम व्यू सेट करें
package com.example.sample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
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);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setCustomView(R.layout.custom_action_bar);
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, "आपने शीर्षक पर क्लिक किया", Toast.LENGTH_LONG).show();
}
});
}
}
चरण 5: मैनिफेस्ट
थीम के साथ मानक मैनिफेस्ट (AppTheme जिसमें ActionBar हो)।
ऐप चलाएं
कस्टम एक्शन बार लोगो, शीर्षक और आइकन के साथ दिखाई देगा।