【问题标题】:How do I remove the space of the actionbar icon?如何删除操作栏图标的空间?
【发布时间】:2014-01-23 08:59:37
【问题描述】:

我使用删除了图标 getActionBar().setDisplayShowHomeEnabled(false);

我正在使用一个应该填满整个操作栏的图像视图。我想这样做是因为我希望图像作为标题栏,包括徽标和文本视图。但是还是希望能够使用actionbar的功能。所以发生的事情是它在图标曾经所在的位置有一个起始间隙。使用 match_parent 或 fill_parent。

标题栏.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="horizontal"
       android:layout_width="fill_parent"
       android:layout_height="match_parent"  
       android:gravity="center_vertical"     
       android:background="@android:color/white">

       <ImageView
              android:id="@+id/header"
              android:contentDescription="@string/app_name"
              android:background="@drawable/ic_launcher"
              android:layout_width="150dp"
              android:layout_height="150dp"
              android:layout_marginStart = "20dp"/>

       <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginStart="40dp"
              android:layout_marginTop = "5dp"
              android:text="@string/titlebarText"
              android:gravity ="right"
              android:textColor="@android:color/black"
              android:textStyle="bold"
              />             
</LinearLayout>

http://i.imgur.com/3eTldKh.png

这就是我想做的动作栏

【问题讨论】:

    标签: android android-actionbar


    【解决方案1】:

    这解决了我的问题:

    MainActivity.java

    // Set up the action bar.
    final ActionBar actionBar = getActionBar();
    //set custom actionbar
    actionBar.setCustomView(R.layout.titlebar);
    //Displays the custom design in the actionbar
    actionBar.setDisplayShowCustomEnabled(true);
    //Turns the homeIcon a View     
    View homeIcon = findViewById(android.R.id.home);
    //Hides the View (and so the icon)
    ((View)homeIcon.getParent()).setVisibility(View.GONE);
    

    标题栏.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
           xmlns:android="http://schemas.android.com/apk/res/android"
           android:orientation="horizontal"
           android:layout_width="fill_parent"
           android:layout_height="match_parent"
           android:gravity="center_vertical"
           android:background="@android:color/white">
    
           <ImageView
                  android:id="@+id/header"
                  android:contentDescription="@string/app_name"
                  android:background="@drawable/ic_launcher"
                  android:layout_width="150dp"
                  android:layout_height="150dp"/>
    
           <TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="@string/titlebarText"
                  android:gravity ="right"
                  android:textColor="@android:color/black"
                  android:textStyle="bold"
                  />             
    </LinearLayout>
    

    【讨论】:

      【解决方案2】:

      删除ImageView中的android:layout_marginStart = "20dp"

      【讨论】:

      • 起初它看起来好像可以工作,但是当我旋转屏幕时,问题又出现了,空间更大。当我在 ImageView 上使用 fill_parent 时,屏幕保持垂直时一半是空的,水平保持时大约 3/4 是空的
      • 当方向改变时,你是否遇到了 textview 右侧空间的问题?
      • 空间在imageview的左边。我正在尝试用更大的徽标和一些文字制作更厚的操作栏。
      • 我添加了一张我希望我的操作栏看起来如何的图片,此图片是自定义标题栏,无法处理操作栏功能
      猜你喜欢
      • 1970-01-01
      • 2014-01-09
      • 2016-09-24
      • 1970-01-01
      • 2015-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多