我想在mainActivity中设置自定义ActionBar。我为名为的操作栏进行了自定义布局custom_header
。并且宽度在custom_layout中是match_parent,并且在Activity中设置Layoutparams也是MATCH_PARENT。但是它仍然在左侧和右侧留有边距。
主要活动
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
ActionBar.LayoutParams params = new ActionBar.LayoutParams(
ActionBar.LayoutParams.MATCH_PARENT,
ActionBar.LayoutParams.MATCH_PARENT
);
View viewActionBar = ((Activity) MainActivity.this).getLayoutInflater().inflate(R.layout.custom_header,null);
actionBar.setCustomView(viewActionBar,params);
custom_header.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:padding="10dp"
android:background="@color/md_white_1000"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:src="@drawable/logo"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_width="40dp"
android:layout_height="40dp" />
<TextView
android:id="@+id/txtHead"
android:text="DIGIM"
android:textColor="@color/colorPrimary"
android:textSize="20sp"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="KEY"
android:textSize="20sp"
android:textColor="@color/appdefault"
android:layout_toRightOf="@+id/txtHead"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageButton
android:src="@drawable/notification_icon"
android:background="@android:color/transparent"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_width="30dp"
android:layout_height="30dp" />
</RelativeLayout>
mainactivity.xml
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:text="@string/title_home"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation" />
</android.support.constraint.ConstraintLayout>
style.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/appdefault</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
只需添加此
Toolbar toolbar=(Toolbar)viewActionBar.getParent();
toolbar.setContentInsetsAbsolute(0,0);
在下面提到的行之后
actionBar.setCustomView(viewActionBar,params);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句