Android操作栏:我可以在appcompat v7中替换自定义标题

时间:2014-07-15 09:19:24

标签: android android-actionbar android-appcompat

我想在肌动蛋白条的左侧添加一个自定义动作标题,替换为默认标题,就像在下图中显示默认图像一样

enter image description here

在这里,我想添加这个标题。

enter image description here

3 个答案:

答案 0 :(得分:3)

您需要更改操作栏中的徽标和标题。

您可以使用

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

getActivity().getActionBar().setLogo(your draw id);

答案 1 :(得分:3)

很容易做到..

如上所述,有很多方法可以执行此任务..但我建议使用操作栏的自定义视图(因为这将适用于所有设备)..这它将让您以非常方便的方式创建更复杂的视图。

对于您的查询,您可以使用ActionBar的自定义视图..

您可以像这样创建自定义操作栏。

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

您还需要设置此属性..

actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

// Now you can do any sort of stuffs with this icon .. like you can perform click functionality

这是你的xyz.xml文件..这个文件仅供参考。这将让你知道如何为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>

你很高兴..

答案 2 :(得分:1)

您需要使用以下代码来实现此目的:

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.
}