【问题标题】:Android Action Bar: Can I replace a custom Title in appcompat v7Android 操作栏:我可以在 appcompat v7 中替换自定义标题吗
【发布时间】:2014-07-15 09:19:24
【问题描述】:

我想在 actin 栏的左侧添加一个自定义动作标题,用默认标题替换,就像在下图中显示的默认图像一样

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

【问题讨论】:

标签: android android-actionbar android-appcompat


【解决方案1】:

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

你可以使用

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

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

【讨论】:

  • 你用这个答案吗?它对你有帮助吗?如果是,请批准他们
【解决方案2】:

这很容易做到..

有很多方法可以完成上面定义的这项任务..但我建议使用操作栏的自定义视图(因为这将在所有设备上都一样)..这个将让您以非常方便和简单的方式创建更复杂的视图。

对于您的查询,您可以使用 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 文件..此文件仅供参考..这将让您知道如何为操作栏创建自定义视图。 :)

<?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>

你很高兴..

【讨论】:

    【解决方案3】:

    你需要使用下面的代码来得到这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-26
      • 2014-12-14
      • 2015-03-24
      • 1970-01-01
      相关资源
      最近更新 更多