【问题标题】:Custom ActionBar setCustomView() has extra margins自定义 ActionBar setCustomView() 有额外的边距
【发布时间】:2017-11-29 06:51:42
【问题描述】:

在设计方面我不是很擅长。我已经有一个ActionBar 我只想自定义它。

正如您在屏幕截图中看到的那样,它没有正确对齐。黄色是默认的ActionBar,灰色是自定义的。我该怎么办?

这是我的xml 代码ActionBar

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/grey_700" >

<TextView
    android:id="@+id/title_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:textAllCaps="true"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#fff"
    android:textStyle="bold" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_marginLeft="8dp"
    app:srcCompat="@drawable/pic" />

<ImageButton
    android:id="@+id/imageButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="8dp"
    android:background="@null"
    android:src="@android:drawable/ic_menu_rotate" />

在我的onCreate()里面

    ActionBar mActionBar = getSupportActionBar();
    mActionBar.setDisplayShowHomeEnabled(false);
    mActionBar.setDisplayShowTitleEnabled(false);
    LayoutInflater mInflater = LayoutInflater.from(this);

    View mCustomView = mInflater.inflate(R.layout.st_actionbar, null);
    TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text);
    mTitleTextView.setText("My Own Title");

    ImageButton imageButton = (ImageButton) mCustomView
            .findViewById(R.id.imageButton);
    imageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        }
    });

    mActionBar.setCustomView(mCustomView);
    mActionBar.setDisplayShowCustomEnabled(true);

【问题讨论】:

    标签: java android android-layout android-actionbar material-design


    【解决方案1】:

    这个ActionBar已经过时了,如果你不用ActionBar我推荐你使用工具栏。你可以去官网看看,https://developer.android.com/reference/android/support/v7/widget/Toolbar.html

    【讨论】:

      猜你喜欢
      • 2013-08-15
      • 2020-04-26
      • 1970-01-01
      • 1970-01-01
      • 2021-08-07
      • 2015-01-15
      • 2012-07-13
      • 2010-12-04
      • 2017-11-05
      相关资源
      最近更新 更多