Android Action Bar: Can I replace a custom Title in appcompat v7

后端 未结 3 1703
逝去的感伤
逝去的感伤 2021-01-21 04:05

I want to add a custom action title on the left of the actin bar, replacing with the default title just like in the below image the default image is shown

相关标签:
3条回答
  • 2021-01-21 04:11

    You need to change the logo and the title in action bar.

    You can use

    getActivity().getActionBar().setTitle("your title");
    

    and

    getActivity().getActionBar().setLogo(your draw id);
    
    0 讨论(0)
  • 2021-01-21 04:17

    It is very easy to do ..

    There's lot's of way to do this task as defined above .. but I will suggest to make use of custom view for Action bar(As this will work on all devices as same) .. this will let you create much more complex view's in a very handy and easy way.

    For your query you make make use of custom views for ActionBar..

    You can create customized action bar like this ..

    actionBar = getSupportActionBar();
    actionBar.setCustomView(R.layout.xyz); // Set custom view like this 
    // In your case you want an icon ... 
    ImageView image = (ImageView) actionBar.getCustomView().findViewById(R.id.fare_ll); // Get id of custom view like this
    

    Also you need to set this property ..

    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    
    // Now you can do any sort of stuffs with this icon .. like you can perform click functionality
    

    Here's your xyz.xml file .. This file is just for your reference purpose .. this will let you know how to create custom view for Action Bar. :)

    <?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="fill_parent"
        android:orientation="horizontal"
    
         >
    
         <LinearLayout 
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_gravity="center|start"
            android:id="@+id/confirm_cabs_back"
            >
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/b"
                />
    
            <LinearLayout 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                 android:id="@+id/city_spinner_layout"
                 android:layout_gravity="center"
                >
    
            <TextView 
                android:id="@+id/action_city"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="XYZ"
                android:textStyle="bold"
                android:textColor="@color/taxi_blue"
                android:layout_gravity="center"
                />
    
            <!-- <ImageView
                android:layout_gravity="center"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/drop_down" /> -->
            </LinearLayout>
    
        </LinearLayout>
    
         <!-- <LinearLayout 
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_gravity="end|center"
        android:gravity="end"
        android:id="@+id/fare_ll"
             > -->
    
        <LinearLayout
            android:layout_width="2dp"
            android:layout_height="fill_parent"
            android:background="@color/grey" >
        </LinearLayout> 
         <!-- android:layout_marginTop="2dp"
            android:layout_marginBottom="2dp" -->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hard_61"
            android:id="@+id/fare_ll"
            android:layout_gravity="center|end"
    
            android:layout_marginLeft="3dp"
            android:layout_marginRight="3dp"
            android:textColor="@color/black"
            />
       <!--  </LinearLayout> -->
    
    </LinearLayout>
    

    You are good to go ..

    0 讨论(0)
  • 2021-01-21 04:32

    You need to use the following code to get this:

    private ActionBar actionBar;
    
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            actionBar = getActionBar();
            actionBar.setTitle("");
            actionBar.setLogo(R.drawable.logo); //logo needs to be defined in the drawables folder.
    }
    
    0 讨论(0)
提交回复
热议问题